String.TrimEnd 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
TrimEnd() |
从当前字符串删除所有尾随空白字符。 |
TrimEnd(Char) |
从当前字符串删除字符的所有尾随匹配项。 |
TrimEnd(Char[]) |
从当前字符串删除数组中指定的一组字符的所有尾随匹配项。 |
TrimEnd(ReadOnlySpan<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
,剪裁操作将停止。 例如,如果 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
参数中的字符后剩余的字符串。 如果 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 及更早版本维护此方法在 为 null
时trimChars
剪裁的内部空白字符列表或空数组。 从 .NET Framework 4 开始,如果 trimChars
为 null
或为空数组,该方法将剪裁所有 Unicode 空白字符, (即在传递给IsWhiteSpace(Char)方法) 时生成true
返回值的字符。 由于此更改,Trim().NET Framework 3.5 SP1 及更早版本中的 方法删除了两个字符:零宽度空格 (U+200B) 和零宽度 NO-BREAK 空格 (U+FEFF) ,.NET Framework Trim() 4 及更高版本中的方法不会删除。 此外,Trim().NET Framework 3.5 SP1 及更早版本中的 方法不会剪裁三个 Unicode 空白字符:MONGOLIAN 元音分隔符 (U+180E) 、NARROW NO-BREAK SPACE (U+202F) 和 MEDIUM MATHEMATICAL SPACE (U+205F) 。
另请参阅
适用于
TrimEnd(ReadOnlySpan<Char>)
public:
System::String ^ TrimEnd(ReadOnlySpan<char> trimChars);
public string TrimEnd (scoped ReadOnlySpan<char> trimChars);
member this.TrimEnd : ReadOnlySpan<char> -> string
Public Function TrimEnd (trimChars As ReadOnlySpan(Of Char)) As String
参数
- trimChars
- ReadOnlySpan<Char>
返回
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈