String.TrimEnd 方法

定義

多載

TrimEnd()

移除目前字串結尾的所有空白字元。

TrimEnd(Char)

移除所有出現在目前字串結尾的字元。

TrimEnd(Char[])

移除出現在目前字串結尾的所有陣列指定字元集。

TrimEnd()

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

移除目前字串結尾的所有空白字元。

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

傳回

從目前字串結尾移除所有空白字元後,遺留下來的字串。 如果在目前的執行個體中無法修剪任何字元,則方法傳回未變更的目前執行個體。

備註

方法 TrimEnd 會從目前字串中移除所有尾端空白字元。 修剪作業會在字串結尾遇到第一個非空白字元時停止。 例如,如果目前的字串是 「abc xyz 」,則 TrimEnd 方法會傳回 「abc xyz」。

注意

TrimEnd如果方法從目前實例移除任何字元,這個方法就不會修改目前實例的值。 相反地,它會傳回新的字串,其中所有尾端空白字元都會從目前的字串中移除。

適用於

TrimEnd(Char)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

移除所有出現在目前字串結尾的字元。

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 字元。

傳回

從目前字串結尾移除所有出現的 trimChar 字元後,遺留下來的字串。 如果在目前的執行個體中無法修剪任何字元,則方法傳回未變更的目前執行個體。

備註

方法 TrimEnd(System.Char) 會從目前字串中移除所有尾端 trimChar 字元。 修剪作業會在字串結尾遇到第 trimChar 一個字元時停止。 例如,如果 is trimChar- 且目前的字串為 「---abc---xyz----」,則 TrimEnd(System.Char) 方法會傳回 「---abc---xyz」。

注意

TrimEnd(System.Char)如果方法從目前實例移除任何字元,這個方法就不會修改目前實例的值。 相反地,它會傳回新的字串,其中所有尾端 trimChar 字元都會從目前的字串中移除。

適用於

TrimEnd(Char[])

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

移除出現在目前字串結尾的所有陣列指定字元集。

public:
 System::String ^ TrimEnd(... cli::array <char> ^ trimChars);
public string TrimEnd (params 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 字元陣列或 null

傳回

從目前的字串結尾處移除 trimChars 參數中所有出現的字元後,所保留下來的字串。 如果 trimCharsnull 或空陣列,則反而會移除 Unicode 空白字元。 如果在目前的執行個體中無法修剪任何字元,則方法傳回未變更的目前執行個體。

範例

下列範例示範如何使用 TrimEnd(System.Char[]) 方法來修剪字串結尾的空白字元或標點符號。

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
let sentence = "The dog had a bone, a ball, and other toys."
let charsToTrim = [| ','; '.'; ' ' |]
let words = sentence.Split()
for word in words do
    printfn $"{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 尾端字元。 修剪作業會在字串結尾遇到非 中的 trimChars 第一個字元時停止。 例如,如果目前的字串為 「123abc456xyz789」,且 trimChars 包含 「1」 到 「9」 的數位,則 TrimEnd(System.Char[]) 方法會傳回 「123abc456xyz」。

注意

TrimEnd(System.Char[])如果方法從目前實例移除任何字元,這個方法就不會修改目前實例的值。 相反地,它會傳回新的字串,其中找到 trimChars 的所有尾端字元都會從目前的字串中移除。

給呼叫者的注意事項

.NET Framework 3.5 SP1 和舊版會維護此方法在 為 null 或空陣列時 trimChars 修剪的內部空白字元清單。 從 .NET Framework 4 開始,如果 為 trimCharsnull 或空陣列,則方法會修剪所有 Unicode 空白字元 (,也就是將傳回值傳遞給 IsWhiteSpace(Char) 方法時產生 true 傳回值的字元) 。 由於這項變更, Trim() .NET Framework 3.5 SP1 和較舊版本中的 方法會移除兩個字元:零寬度空格 (U+200B) 和零寬度 NO-BREAK SPACE (U+FEFF) , Trim() .NET Framework 4 和更新版本中的方法不會移除。 此外, Trim() .NET Framework 3.5 SP1 和較舊版本中的 方法不會修剪三個 Unicode 空白字元:在 U+180E (U+180E) 、NARROW NO-BREAK SPACE (U+202F) ,以及中數學空間 (U+205F) 。

另請參閱

適用於