trim();
void
DelSpace()
{
string strWithSpace = " this is a string with space " ;
string strWithoutSpace = strWithSpace.Trim();
Console.WriteLine(strWithoutSpace);
Console.ReadKey();
}
{
string strWithSpace = " this is a string with space " ;
string strWithoutSpace = strWithSpace.Trim();
Console.WriteLine(strWithoutSpace);
Console.ReadKey();
}
上面的程序的作用是将字符串strWithSpace前后的空格去掉,输出"this is a string with space"。
trimStart();
用于删除字符串前面的空格
void
DelSpace()
{
string strWithSpace = " this is a string with space " ;
string strWithoutSpace = strWithSpace.TrimStart();
Console.WriteLine(strWithoutSpace);
Console.ReadKey();
}
{
string strWithSpace = " this is a string with space " ;
string strWithoutSpace = strWithSpace.TrimStart();
Console.WriteLine(strWithoutSpace);
Console.ReadKey();
}
上面的程序的作用是将字符串strWithSpace前面的空格去掉,输入"this is a string with space "
trimEnd();
void
DelSpace()
{
string strWithSpace = " this is a string with space " ;
string strWithoutSpace = strWithSpace.TrimEnd();
Console.WriteLine(strWithoutSpace);
Console.ReadKey();
}
{
string strWithSpace = " this is a string with space " ;
string strWithoutSpace = strWithSpace.TrimEnd();
Console.WriteLine(strWithoutSpace);
Console.ReadKey();
}
上面的程序输出" this is a string with space"
删除指定的字符
Trim()方法还可以删除指定的字符串
- char[] trimChars = {" ","e","g"};
- string strA = strA.trim(trimChars);