String.TrimEnd 方法

定义

重载

TrimEnd(Char[])

从当前字符串删除数组中指定的一组字符的所有尾随匹配项。Removes all the trailing occurrences of a set of characters specified in an array from the current string.

TrimEnd(Char)

从当前字符串删除字符的所有尾随匹配项。Removes all the trailing occurrences of a character from the current string.

TrimEnd()

从当前字符串删除所有尾随空白字符。Removes all the trailing white-space characters from the current string.

TrimEnd(Char[])

从当前字符串删除数组中指定的一组字符的所有尾随匹配项。Removes all the trailing occurrences of a set of characters specified in an array from the current string.

public:
 System::String ^ TrimEnd(... cli::array <char> ^ trimChars);
public string TrimEnd (params char[] trimChars);
member this.TrimEnd : char[] -> string
Public Function TrimEnd (ParamArray trimChars As Char()) As String

参数

trimChars
Char[]

要删除的 Unicode 字符的数组,或 nullAn array of Unicode characters to remove, or null.

返回

从当前字符串的开头移除所出现的所有 trimChars 参数中的字符后剩余的字符串。The string that remains after all occurrences of the characters in the trimChars parameter are removed from the end of the current string. 如果 trimCharsnull 或空数组,则改为删除 Unicode 空白字符。If trimChars is null or an empty array, Unicode white-space characters are removed instead. 如果从当前实例无法删除字符,此方法返回未更改的当前实例。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.

示例

下面的示例演示如何使用 TrimEnd(System.Char[]) 方法从字符串末尾裁剪空格或标点符号。The following example demonstrates how you can use the TrimEnd(System.Char[]) method to trim white space or punctuation marks from the end of a string.

string sentence = "The dog had a bone, a ball, and other toys.";
char[] charsToTrim = {',', '.', ' '};
string[] words = sentence.Split();
foreach (string word in words)
   Console.WriteLine(word.TrimEnd(charsToTrim));

// The example displays the following output:
//       The
//       dog
//       had
//       a
//       bone
//       a
//       ball
//       and
//       other
//       toys
Module TrimEnd
   Public Sub Main()
      Dim sentence As String = "The dog had a bone, a ball, and other toys."
      Dim charsToTrim() As Char = {","c, "."c, " "c}
      Dim words() As String = sentence.Split()
      For Each word As String In words
         Console.WriteLine(word.TrimEnd(charsToTrim))
      Next
   End Sub
End Module
' The example displays the following output:
'       The
'       dog
'       had
'       a
'       bone
'       a
'       ball
'       and
'       other
'       toys

注解

TrimEnd(System.Char[]) 方法从当前字符串中删除 trimChars 参数中的所有尾随字符。The TrimEnd(System.Char[]) method removes from the current string all trailing characters that are in the trimChars parameter. 在字符串末尾遇到不在 trimChars 中的第一个字符时,剪裁操作将停止。The trim operation stops when the first character that is not in trimChars is encountered at the end of the string. 例如,如果当前字符串为 "123abc456xyz789",并且 trimChars 包含从 "1" 到 "9" 的数字,则 TrimEnd(System.Char[]) 方法返回 "123abc456xyz"。For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the TrimEnd(System.Char[]) method returns "123abc456xyz".

备注

如果 TrimEnd(System.Char[]) 方法从当前实例中删除任何字符,则此方法不会修改当前实例的值。If the TrimEnd(System.Char[]) method removes any characters from the current instance, this method does not modify the value of the current instance. 相反,它会返回一个新字符串,其中在 trimChars 中找到的所有尾随字符将从当前字符串中删除。Instead, it returns a new string in which all trailing characters found in trimChars are removed from the current string.

调用方说明

.NET Framework 3.5 SP1.NET Framework 3.5 SP1 及更早版本维护在 trimChars null 或空数组时此方法修整的空白字符的内部列表。The .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions maintains an internal list of white-space characters that this method trims if trimChars is null or an empty array. .NET Framework 4.NET Framework 4开始,如果 trimChars null 或空数组,则该方法将剪裁所有 Unicode 空白字符(也就是说,在将其传递到 IsWhiteSpace(Char) 方法时生成 true 返回值的字符)。Starting with the .NET Framework 4.NET Framework 4, if trimChars is null or an empty array, the method trims all Unicode white-space characters (that is, characters that produce a true return value when they are passed to the IsWhiteSpace(Char) method). 由于此更改,.NET Framework 3.5 SP1.NET Framework 3.5 SP1 和更早版本中的 Trim() 方法将删除两个字符:零宽度空间(U + 200B)和零宽度无间断空格(U + FEFF),.NET Framework 4.NET Framework 4 和更高版本中的 Trim() 方法不会删除。Because of this change, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions removes two characters, ZERO WIDTH SPACE (U+200B) and ZERO WIDTH NO-BREAK SPACE (U+FEFF), that the Trim() method in the .NET Framework 4.NET Framework 4 and later versions does not remove. 此外,.NET Framework 3.5 SP1.NET Framework 3.5 SP1 及更早版本中的 Trim() 方法不会剪裁三个 Unicode 空白字符:蒙古语元音分隔符(U + 180E)、窄的无中断空格(U + 202F)和中等数学空间(U + 205F)。In addition, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions does not trim three Unicode white-space characters: MONGOLIAN VOWEL SEPARATOR (U+180E), NARROW NO-BREAK SPACE (U+202F), and MEDIUM MATHEMATICAL SPACE (U+205F).

另请参阅

TrimEnd(Char)

从当前字符串删除字符的所有尾随匹配项。Removes all the trailing occurrences of a character from the current string.

public:
 System::String ^ TrimEnd(char trimChar);
public string TrimEnd (char trimChar);
member this.TrimEnd : char -> string
Public Function TrimEnd (trimChar As Char) As String

参数

trimChar
Char

要删除的 Unicode 字符。A Unicode character to remove.

返回

从当前字符串的末尾删除所出现的所有 trimChar 字符后剩余的字符串。The string that remains after all occurrences of the trimChar character are removed from the end of the current string. 如果从当前实例无法删除字符,此方法返回未更改的当前实例。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.

注解

TrimEnd(System.Char) 方法从当前字符串中删除所有尾随 trimChar 字符。The TrimEnd(System.Char) method removes from the current string all trailing trimChar characters. 在字符串末尾遇到不 trimChar 的第一个字符时,剪裁操作将停止。The trim operation stops when the first character that is not trimChar is encountered at the end of the string. 例如,如果 trimChar - 并且当前字符串为 "---abc---xyz----",则 TrimEnd(System.Char) 方法返回 "---abc---xyz"。For example, if trimChar is - and the current string is "---abc---xyz----", the TrimEnd(System.Char) method returns "---abc---xyz".

备注

如果 TrimEnd(System.Char) 方法从当前实例中删除任何字符,则此方法不会修改当前实例的值。If the TrimEnd(System.Char) method removes any characters from the current instance, this method does not modify the value of the current instance. 相反,它会返回一个新字符串,其中所有尾随 trimChar 字符都从当前字符串中删除。Instead, it returns a new string in which all trailing trimChar characters are removed from the current string.

TrimEnd()

从当前字符串删除所有尾随空白字符。Removes all the trailing white-space characters from the current string.

public:
 System::String ^ TrimEnd();
public string TrimEnd ();
member this.TrimEnd : unit -> string
Public Function TrimEnd () As String

返回

从当前字符串的结尾删除所有空白字符后剩余的字符串。The string that remains after all white-space characters are removed from the end of the current string. 如果从当前实例无法删除字符,此方法返回未更改的当前实例。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.

注解

TrimEnd 方法从当前字符串中删除所有尾随空格字符。The TrimEnd method removes from the current string all trailing white-space characters. 当在字符串末尾遇到第一个非空白字符时,剪裁操作将停止。The trim operation stops when the first non white-space character is encountered at the end of the string. 例如,如果当前字符串为 "abc xyz",则 TrimEnd 方法返回 "abc xyz"。For example, if the current string is " abc xyz ", the TrimEnd method returns " abc xyz".

备注

如果 TrimEnd 方法从当前实例中删除任何字符,则此方法不会修改当前实例的值。If the TrimEnd method removes any characters from the current instance, this method does not modify the value of the current instance. 相反,它会返回一个新字符串,其中所有尾随空格字符都从当前字符串中删除。Instead, it returns a new string in which all trailing white-space characters are removed from the current string.

适用于