String.TrimEnd String.TrimEnd String.TrimEnd String.TrimEnd Method

定義

多載

TrimEnd(Char[]) TrimEnd(Char[]) TrimEnd(Char[]) TrimEnd(Char[])

從目前的 String 物件中移除陣列中指定之一組字元的所有結尾指定項目。Removes all trailing occurrences of a set of characters specified in an array from the current String object.

TrimEnd(Char) TrimEnd(Char) TrimEnd(Char) TrimEnd(Char)
TrimEnd() TrimEnd() TrimEnd() TrimEnd()

TrimEnd(Char[]) TrimEnd(Char[]) TrimEnd(Char[]) TrimEnd(Char[])

從目前的 String 物件中移除陣列中指定之一組字元的所有結尾指定項目。Removes all trailing occurrences of a set of characters specified in an array from the current String object.

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(Char[])方法,從字串的結尾修剪空白字元或標點符號。The following example demonstrates how you can use the TrimEnd(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

備註

方法會從目前字串trimChars中移除參數中所有尾端的字元。 TrimEndThe TrimEnd 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方法會傳回 "123abc456xyz"。For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the TrimEnd method returns "123abc456xyz".

注意

TrimEnd如果方法從目前的實例移除任何字元,這個方法就不會修改目前實例的值。If the TrimEnd 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更早的版本會維護此方法在為null或空陣列時trimChars ,所修剪之空白字元的內部清單。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. null IsWhiteSpace(Char) true從開始trimChars ,如果是或空陣列,則方法會修剪所有 Unicode 空白字元(也就是在傳遞至方法時產生傳回值的字元)。 .NET Framework 4.NET Framework 4Starting 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). 由於Trim()這項變更, .NET Framework 3.5 SP1.NET Framework 3.5 SP1和較舊版本中的方法會移除兩個字元:零寬度空格(u + 200B)和零寬度不中斷空間(u + FEFF), .NET Framework 4.NET Framework 4Trim()和更新版本中的方法會執行不移除。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) TrimEnd(Char) TrimEnd(Char) TrimEnd(Char)

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 Char Char Char

傳回

TrimEnd() TrimEnd() TrimEnd() TrimEnd()

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

傳回

適用於