第1个回答 推荐于2018-02-28
你在开发工具上,点Trim这个方法会提示它的功能.其它方法也一样.活学活用
这个方法就是删除字符串中前尾空格.
另外这个方法还有重载即String.Trim(Param char[])
例:string s=" *abc# ";
s=s.Trim()后,s="*abc#";
s=s.Trim('*','#')后,s="abc";实际上你在括号内,可以加上更多你想要去掉的前尾字符.
如果想要去掉字符串中所有空格,请用s =s.Replace(" ","");本回答被网友采纳
第2个回答 2008-11-02
C#
public string Trim ()
C++
public:
String^ Trim ()
J#
public String Trim ()
JScript
public function Trim () : String
返回值
一个新 String,相当于将此实例的首尾空白字符移除后形成的字符串。
备注
下表列出了被 Trim 方法移除的空白字符。第一列列出了字符的 Unicode 名称,第二列列出了标识该字符的码位的 Unicode 十六进制表示法。
Unicode 名称
Unicode 码位
CHARACTER TABULATION
U+0009
LINE FEED
U+000A
LINE TABULATION
U+000B
FORM FEED
U+000C
CARRIAGE RETURN
U+000D
SPACE
U+0020
NO-BREAK SPACE
U+00A0
EN QUAD
U+2000
EM QUAD
U+2001
EN SPACE
U+2002
EM SPACE
U+2003
THREE-PER-EM SPACE
U+2004
FOUR-PER-EM SPACE
U+2005
SIX-PER-EM SPACE
U+2006
FIGURE SPACE
U+2007
PUNCTUATION SPACE
U+2008
THIN SPACE
U+2009
HAIR SPACE
U+200A
ZERO WIDTH SPACE
U+200B
IDEOGRAPHIC SPACE
U+3000
ZERO WIDTH NO-BREAK SPACE
U+FEFF
下面的代码示例演示 Trim(Char[]) 方法重载。
Visual Basic 复制代码
Imports System
Public Class TrimTest
Public Shared Sub Main()
Dim temp As String() = MakeArray()
Console.WriteLine("Concatenating the inital values in the array, we get the string:")
Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
Dim i As Integer
' trim whitespace from both ends of the elements
For i = 0 To temp.Length - 1
temp(i) = temp(i).Trim()
Next i
Console.WriteLine("Concatenating the trimmed values in the array, we get the string:")
Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
' reset the array
temp = MakeArray()
' trim the start of the elements. Passing null trims whitespace only
For i = 0 To temp.Length - 1
temp(i) = temp(i).TrimStart(" "c)
Next i
Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:")
Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
' reset the array
temp = MakeArray()
' trim the end of the elements. Passing null trims whitespace only
For i = 0 To temp.Length - 1
temp(i) = temp(i).TrimEnd(" "c)
Next i
Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:")
Console.WriteLine("'{0}'", [String].Concat(temp))
End Sub 'Main
Private Shared Function MakeArray() As String()
Dim arr As String() = {" please ", " tell ", " me ", " about ", " yourself "}
Return arr
End Function 'MakeArray
End Class 'TrimTest
C# 复制代码
using System;
public class TrimTest {
public static void Main() {
string [] temp = MakeArray();
Console.WriteLine("Concatenating the inital values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// trim whitespace from both ends of the elements
for (int i = 0; i < temp.Length; i++)
temp[i] = temp[i].Trim();
Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// reset the array
temp = MakeArray();
// trim the start of the elements. Passing null trims whitespace only
for (int i = 0; i < temp.Length; i++)
temp[i] = temp[i].TrimStart(null);
Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// reset the array
temp = MakeArray();
// trim the end of the elements. Passing null trims whitespace only
for (int i = 0; i < temp.Length; i++)
temp[i] = temp[i].TrimEnd(null);
Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'", String.Concat(temp));
}
private static string [] MakeArray() {
string [] arr = {" please ", " tell ", " me ", " about ", " yourself "};
return arr;
}
}
要学会MSDN啊