String.TrimEnd 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| TrimEnd(Rune) | |
| TrimEnd() |
从当前字符串中删除所有尾随空格字符。 |
| TrimEnd(Char) |
从当前字符串中删除字符的所有尾随匹配项。 |
| TrimEnd(Char[]) |
从当前字符串中删除数组中指定的一组字符的所有尾随匹配项。 |
TrimEnd()
- Source:
- String.Manipulation.cs
- 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
- 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 时停止。 例如,如果 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
- 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 所有字符出现后保留的字符串将从当前字符串的末尾删除。 如果 trimChars 为 null 空数组或空数组,则改为删除 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 开始,如果trimChars为null或为空数组,该方法将剪裁所有 Unicode 空格字符(也就是说,在传递给IsWhiteSpace(Char)方法时生成true返回值的字符)。 由于此更改, Trim() .NET Framework 3.5 SP1 和早期版本中的方法删除了两个字符:零宽度空间(U+200B)和零宽度 NO-BREAK 空格(U+FEFF), Trim() .NET Framework 4 及更高版本中的方法不会删除。 此外, Trim() .NET Framework 3.5 SP1 和早期版本中的方法不会剪裁三个 Unicode 空格字符:MONGOLIAN VOWEL SEPARATOR(U+180E)、NARROW NO-BREAK SPACE(U+202F)和中等数学空间(U+205F)。