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.

適用於