C#中的String.Trim()的功能是什么?

回答得越好懂,越详细越好,谢谢了!

String.Trim(Char[])如下,无参数的Trim方法相当于参数是空格:
从当前 String 对象移除数组中指定的一组字符的所有前导匹配项和尾部匹配项。

命名空间: System
程序集: mscorlib(在 mscorlib.dll 中)

语法
Visual Basic(声明)
Public Function Trim ( _
trimChars As Char() _
) As String

Visual Basic(用法)
Dim instance As String
Dim trimChars As Char()
Dim returnValue As String

returnValue = instance.Trim(trimChars)

C#
public string Trim(
char[] trimChars
)

Visual C++
public:
String^ Trim(
array<wchar_t>^ trimChars
)

J#
public String Trim(
char[] trimChars
)

JScript
public function Trim(
trimChars : char[]
) : String

参数
trimChars
类型:array<System..::.Char>[]()[]

要移除的 Unicode 字符数组或 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。

返回值
类型:System..::.String

从当前 String 对象的开始和末尾移除 trimChars 参数中字符的所有匹配项后保留的字符串。如果 trimChars 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),则改为移除空白字符。

备注
Trim 方法从当前字符串移除 trimChars 参数中的所有前导字符和尾部字符。遇到不在 trimChars 中的字符时,每个前导裁剪操作和尾部裁剪操作都会停止。例如,如果当前字符串为“123abc456xyz789”并且 trimChars 包含从“1”到“9”的数字,则 Trim 方法返回“abc456xyz”。

有关将哪些 Unicode 字符归类为空白字符的更多信息,请参见 String..::.Trim()()() 方法重载的“备注”部分。

示例
下面的代码示例演示 Trim(array<Char>[]()[]) 方法重载。

Visual Basic 复制代码
Imports System
_

Class stringTrim2

Public Shared Sub Main()
Dim str1 As [String] = "*;|@123***456@|;*"
Dim delim As [String] = "*;|@"
Dim str2 As [String] = str1.Trim(delim.ToCharArray())

Console.WriteLine("Delimiters: {0}", delim)
Console.WriteLine("Original string: {0}", str1)
Console.WriteLine("Trimmed string: {0}", str2)
End Sub 'Main
End Class 'stringTrim2

' This code example displays the following:
'
' Delimiters: *;|@
' Original string: *;|@123***456@|;*
' Trimmed string: 123***456

C# 复制代码
using System;

class stringTrim2 {
public static void Main() {
String str1 = "*;|@123***456@|;*";
String delim = "*;|@";
String str2 = str1.Trim(delim.ToCharArray());

Console.WriteLine("Delimiters: {0}", delim);
Console.WriteLine("Original string: {0}", str1);
Console.WriteLine("Trimmed string: {0}", str2);
}
}
// This code example displays the following:
//
// Delimiters: *;|@
// Original string: *;|@123***456@|;*
// Trimmed string: 123***456
温馨提示:答案为网友推荐,仅供参考
第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啊
第3个回答  2008-11-03
String.Trim();主要做用是去了字符串前后空格.
例如:
string temp = " fgfdg " //定义一个字符串

temp = temp.Trim(); //去掉temp前后的空格后重新赋值给变量temp

Console.WriteLine(temp); //打印得到fgfdg了,就没有了空格了.
第4个回答  2008-11-03
......那位仁兄好能贴.....
我给你个精简版的,但是绝对准确

String.Trim () 从此实例的开始位置和末尾移除空白字符的所有匹配项。
String.Trim (Char[]) 从此实例的开始和末尾移除数组中指定的一组字符的所有匹配项。

参考资料:MSDN

相似回答