添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
System::String ^ Trim(char trimChar);
public string Trim (char trimChar);
member this.Trim : char -> string
Public Function Trim (trimChar As Char) As String

方法 Trim(System.Char) 从当前字符串中删除字符的所有前导实例和尾随实例 trimChar 。 遇到不同于 trimChar 的字符时,每个前导和尾随剪裁操作都会停止。 例如,如果 trimChar 为 - 且当前字符串为“---abc---xyz----”,则 Trim(System.Char) 该方法返回“abc---xyz”。 若要删除字符串中单词之间的字符,请使用 .NET 正则表达式 。

如果 方法 Trim(System.Char) 从当前实例中删除任何字符,则此方法不会修改当前实例的值。 相反,它将返回一个新字符串,其中在当前实例中找到的所有前导字符和尾随 trimChar 字符都将被删除。

如果当前字符串等于 Empty 或当前实例中的所有字符都由字符组成 trimChar ,则 方法返回 Empty 。

public:
 System::String ^ Trim(ReadOnlySpan<char> trimChars);
public string Trim (ReadOnlySpan<char> trimChars);
member this.Trim : ReadOnlySpan<char> -> string
Public Function Trim (trimChars As ReadOnlySpan(Of Char)) As String
String^ firstName = Console::ReadLine(); Console::Write("Enter your middle name or initial: "); String^ middleName = Console::ReadLine(); Console::Write("Enter your last name: "); String^ lastName = Console::ReadLine(); Console::WriteLine(); Console::WriteLine("You entered '{0}', '{1}', and '{2}'.", firstName, middleName, lastName); String^ name = ((firstName->Trim() + " " + middleName->Trim())->Trim() + " " + lastName->Trim())->Trim(); Console::WriteLine("The result is " + name + "."); // The following is possible output from this example: // Enter your first name: John // Enter your middle name or initial: // Enter your last name: Doe // You entered ' John ', '', and ' Doe'. // The result is John Doe. using System; public class Example public static void Main() Console.Write("Enter your first name: "); string firstName = Console.ReadLine(); Console.Write("Enter your middle name or initial: "); string middleName = Console.ReadLine(); Console.Write("Enter your last name: "); string lastName = Console.ReadLine(); Console.WriteLine(); Console.WriteLine("You entered '{0}', '{1}', and '{2}'.", firstName, middleName, lastName); string name = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " + lastName.Trim()).Trim(); Console.WriteLine("The result is " + name + "."); // The following is a possible output from this example: // Enter your first name: John // Enter your middle name or initial: // Enter your last name: Doe // You entered ' John ', '', and ' Doe'. // The result is John Doe. printf "Enter your first name: " let firstName = stdin.ReadLine() printf "Enter your middle name or initial: " let middleName = stdin.ReadLine() printf "Enter your last name: " let lastName = stdin.ReadLine() printfn $"\nYou entered '{firstName}', '{middleName}', and '{lastName}'." let name = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " + lastName.Trim()).Trim() printfn $"The result is {name}." // The following is a possible output from this example: // Enter your first name: John // Enter your middle name or initial: // Enter your last name: Doe // You entered ' John ', '', and ' Doe'. // The result is John Doe. Module Example Public Sub Main() Console.Write("Enter your first name: ") Dim firstName As String = Console.ReadLine() Console.Write("Enter your middle name or initial: ") Dim middleName As String = Console.ReadLine() Console.Write("Enter your last name: ") Dim lastName As String = Console.ReadLine Console.WriteLine() Console.WriteLine("You entered '{0}', '{1}', and '{2}'.", _ firstName, middleName, lastName) Dim name As String = ((firstName.Trim() + " " + middleName.Trim()).Trim() _ + " " + lastName.Trim()).Trim() Console.WriteLine("The result is " + name + ".") End Sub End Module ' The following is possible output from this example: ' Enter your first name: John ' Enter your middle name or initial: ' Enter your last name: Doe ' You entered ' John ', '', and ' Doe'. ' The result is John Doe.

方法 Trim 从当前字符串中删除所有前导空格和尾随空格字符。 遇到非空格字符时,每个前导和尾随剪裁操作都会停止。 例如,如果当前字符串为“abc xyz”,则 Trim 方法返回“abc xyz”。 若要删除字符串中单词之间的空格字符,请使用 .NET 正则表达式 。

如果 方法 Trim 从当前实例中删除任何字符,则此方法不会修改当前实例的值。 相反,它会返回一个新字符串,其中将删除在当前实例中找到的所有前导空格和尾随空格字符。

如果当前字符串等于 Empty 或当前实例中的所有字符都由空格字符组成,则 方法返回 Empty 。

空格字符由 Unicode 标准定义。 方法 Trim 删除任何前导字符和尾随字符,这些字符在传递给 Char.IsWhiteSpace 方法时会生成 返回值 true 。

调用方说明

.NET Framework 3.5 SP1 及更早版本维护此方法剪裁的空白字符的内部列表。 从 .NET Framework 4 开始,该方法将剪裁所有 Unicode 空格字符 (即在传递给 IsWhiteSpace(Char) 方法) 时生成 true 返回值的字符。 由于此更改, Trim() .NET Framework 3.5 SP1 及更早版本中的方法删除了两个字符,即零宽度空间 (U+200B) 和零宽度无中断空间 (U+FEFF) ,而 Trim() .NET Framework 4 及更高版本中的方法不会删除。 此外, Trim() .NET Framework 3.5 SP1 及更早版本中的方法不会剪裁三个 Unicode 空格字符:MONGOLIAN VOWEL SEPARATOR (U+180E) 、NARROW NO-BREAK SPACE (U+202F) 和 MEDIUM MATHEMATICAL SPACE (U+205F) 。

public:
 System::String ^ Trim(... cli::array <char> ^ trimChars);
public string Trim (params char[] trimChars);
public string Trim (params char[]? trimChars);
member this.Trim : char[] -> string
Public Function Trim (ParamArray trimChars As Char()) As String
array<Char>^ charsToTrim = { L'*', L' ', L'\\' }; String^ banner = "*** Much Ado About Nothing ***"; String^ result = banner->Trim(charsToTrim); Console::WriteLine("Trimmmed\n {0}\nto\n '{1}'", banner, result); // The example displays the following output: // Trimmmed // *** Much Ado About Nothing *** // to // 'Much Ado About Nothing' char[] charsToTrim = { '*', ' ', '\''}; string banner = "*** Much Ado About Nothing ***"; string result = banner.Trim(charsToTrim); Console.WriteLine("Trimmed\n {0}\nto\n '{1}'", banner, result); // The example displays the following output: // Trimmed // *** Much Ado About Nothing *** // to // 'Much Ado About Nothing' let charsToTrim = [| '*'; ' '; '\'' |] let banner = "*** Much Ado About Nothing ***" let result = banner.Trim charsToTrim printfn $"Trimmmed\n {banner}\nto\n '{result}'" // The example displays the following output: // Trimmmed // *** Much Ado About Nothing *** // to // 'Much Ado About Nothing' Module Example Public Sub Main() Dim charsToTrim() As Char = { "*"c, " "c, "'"c} Dim banner As String = "*** Much Ado About Nothing ***" Dim result As String = banner.Trim(charsToTrim) Console.WriteLine("Trimmmed{0} {1}{0}to{0} '{2}'", _ vbCrLf, banner, result) End Sub End Module ' The example displays the following output: ' Trimmmed ' *** Much Ado About Nothing *** ' to ' 'Much Ado About Nothing'

方法 Trim(System.Char[]) 从当前字符串中删除 参数中的所有 trimChars 前导字符和尾随字符。 遇到不在 中的 trimChars 字符时,每个前导和尾随剪裁操作都会停止。 例如,如果当前字符串为“123abc456xyz789”,并且 trimChars 包含从“1”到“9”的数字,则 Trim(System.Char[]) 该方法返回“abc456xyz”。

如果 方法 Trim(System.Char[]) 从当前实例中删除任何字符,则此方法不会修改当前实例的值。 相反,它将返回一个新字符串,其中在当前实例中找到的所有前导字符和尾随 trimChars 字符都将被删除。

如果当前字符串等于 Empty 或当前实例中的所有字符都由 数组中的 trimChars 字符组成,则 方法返回 Empty 。

如果 trimChars 为 null 或空数组,此方法将删除导致方法在传递到 Char.IsWhiteSpace 方法时返回 true 的任何前导字符或尾随字符。

调用方说明

.NET Framework 3.5 SP1 及更早版本维护此方法剪裁的空白字符的内部列表(如果 trimChars 为 null )或空数组。 从 .NET Framework 4 开始,如果 trimChars 为 null 或空数组,该方法将剪裁所有 Unicode 空格字符 (即,在传递给 IsWhiteSpace(Char) 方法时生成 true 返回值的字符) 。 由于此更改, Trim() .NET Framework 3.5 SP1 及更早版本中的方法删除了两个字符:零宽度空间 (U+200B) 和零宽度无中断空间 (U+FEFF) ,而 Trim() .NET Framework 4 及更高版本中的方法不会删除。 此外, Trim() .NET Framework 3.5 SP1 及更早版本中的方法不会剪裁三个 Unicode 空格字符:MONGOLIAN VOWEL SEPARATOR (U+180E) 、NARROW NO-BREAK SPACE (U+202F) 和 MEDIUM MATHEMATICAL SPACE (U+205F) 。

即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅: https://aka.ms/ContentUserFeedback 。

提交和查看相关反馈