共用方式為


String.TrimEnd 方法

定義

多載

名稱 Description
TrimEnd(Rune)
TrimEnd()

從目前字串中移除所有尾端空格符。

TrimEnd(Char)

從目前字串中移除字元的所有尾端專案。

TrimEnd(Char[])

從目前字串中移除陣列中指定的一組字元的所有尾端專案。

TrimEnd(Rune)

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

參數

trimRune
Rune

傳回

適用於

TrimEnd()

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
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)

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
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 出現的字元時,修剪操作即停止。 例如,若 trimChar- 目前字串為「---abc---xyz----」,則該 TrimEnd(System.Char) 方法回傳「---abc---xyz」。

注意

若方法 TrimEnd(System.Char) 移除當前實例中的任何字元,該方法不會改變當前實例的值。 取而代之的是,它會回傳一個新字串,該字串中所有尾隨 trimChar 字元都被移除。

適用於

TrimEnd(Char[])

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
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 及更早版本維護一個內部的空白字元清單,若 trimChars 為 或 null 為空陣列,此方法會修剪。 從 .NET Framework 4 開始,若trimCharsnull空陣列,該方法會修剪所有 Unicode 空白字元(即傳遞給IsWhiteSpace(Char)方法時會產生true回傳值的字元)。 由於此變更, Trim() .NET Framework 3.5 SP1 及更早版本中的方法移除了兩個字元:零寬度空間(U+200B)與零寬度 NO-BREAK 空間(U+FEFF),而 Trim() .NET Framework 4 及以後版本的方法則未移除這些字元。 此外,.NET Framework 3.5 SP1 及更早版本中的方法 Trim() 不會裁剪三個 Unicode 空白字元:蒙古元音分隔符(U+180E)、狹 NO-BREAK 空間(U+202F)和 MEDIUM MATHEMATICAL SPACE(U+205F)。

另請參閱

適用於