String.Trim 方法

定义

返回一个新字符串,它相当于从当前字符串中删除了一组指定字符的所有前导匹配项和尾随匹配项。Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current string are removed.

重载

Trim(Char[])

从当前字符串删除数组中指定的一组字符的所有前导匹配项和尾随匹配项。Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.

Trim(Char)

从当前字符串删除字符的所有前导实例和尾随实例。Removes all leading and trailing instances of a character from the current string.

Trim()

从当前字符串删除所有前导空白字符和尾随空白字符。Removes all leading and trailing white-space characters from the current string.

Trim(Char[])

从当前字符串删除数组中指定的一组字符的所有前导匹配项和尾随匹配项。Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.

public:
 System::String ^ Trim(... cli::array <char> ^ trimChars);
public string Trim (params char[] trimChars);
member this.Trim : char[] -> string
Public Function Trim (ParamArray trimChars As Char()) As String

参数

trimChars
Char[]

要删除的 Unicode 字符的数组,或 nullAn array of Unicode characters to remove, or null.

返回

从当前字符串的开头移除所出现的所有 trimChars 参数中的字符后剩余的字符串。The string that remains after all occurrences of the characters in the trimChars parameter are removed from the start and end of the current string. 如果 trimCharsnull 或空数组,则改为移除空白字符。If trimChars is null or an empty array, white-space characters are removed instead. 如果从当前实例无法删除字符,此方法返回未更改的当前实例。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.

示例

下面的示例使用 Trim(System.Char[]) 方法从字符串中删除空格、星号(*)和撇号(')字符。The following example uses the Trim(System.Char[]) method to remove space, asterisk (*), and apostrophe (') characters from a string.

using namespace System;

void main()
{
   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("Trimmmed\n   {0}\nto\n   '{1}'", banner, 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 参数中的所有前导和尾随字符。The Trim(System.Char[]) method removes from the current string all leading and trailing characters that are in the trimChars parameter. 当遇到不在 trimChars 中的字符时,将停止每个前导和尾随剪裁操作。Each leading and trailing trim operation stops when a character that is not in trimChars is encountered. 例如,如果当前字符串为 "123abc456xyz789",并且 trimChars 包含从 "1" 到 "9" 的数字,则 Trim(System.Char[]) 方法返回 "abc456xyz"。For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the Trim(System.Char[]) method returns "abc456xyz".

备注

如果 Trim(System.Char[]) 方法从当前实例中删除任何字符,则此方法不会修改当前实例的值。If the Trim(System.Char[]) method removes any characters from the current instance, this method does not modify the value of the current instance. 相反,它会返回一个新字符串,其中将删除当前实例中找到的所有前导和尾随 trimChars 字符。Instead, it returns a new string in which all leading and trailing trimChars characters found in the current instance are removed.

如果当前字符串等于 Empty 或当前实例中的所有字符都包含 trimChars 数组中的字符,则该方法返回 EmptyIf the current string equals Empty or all the characters in the current instance consist of characters in the trimChars array, the method returns Empty.

如果 trimChars null 或空数组,则此方法将删除在将方法传递到 Char.IsWhiteSpace 方法时导致方法返回 true 的所有前导或尾随字符。If trimChars is null or an empty array, this method removes any leading or trailing characters that result in the method returning true when they are passed to the Char.IsWhiteSpace method.

调用方说明

.NET Framework 3.5 SP1.NET Framework 3.5 SP1 及更早版本维护在 trimChars null 或空数组时此方法修整的空白字符的内部列表。The .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions maintains an internal list of white-space characters that this method trims if trimChars is null or an empty array. .NET Framework 4.NET Framework 4开始,如果 trimChars null 或空数组,则该方法将剪裁所有 Unicode 空白字符(也就是说,在将其传递到 IsWhiteSpace(Char) 方法时生成 true 返回值的字符)。Starting with the .NET Framework 4.NET Framework 4, if trimChars is null or an empty array, the method trims all Unicode white-space characters (that is, characters that produce a true return value when they are passed to the IsWhiteSpace(Char) method). 由于此更改,.NET Framework 3.5 SP1.NET Framework 3.5 SP1 和更早版本中的 Trim() 方法将删除两个字符:零宽度空间(U + 200B)和零宽度无间断空格(U + FEFF),.NET Framework 4.NET Framework 4和更高版本中的 Trim() 方法不会删除。Because of this change, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions removes two characters, ZERO WIDTH SPACE (U+200B) and ZERO WIDTH NO-BREAK SPACE (U+FEFF), that the Trim() method in the .NET Framework 4.NET Framework 4and later versions does not remove. 此外,.NET Framework 3.5 SP1.NET Framework 3.5 SP1 及更早版本中的 Trim() 方法不会剪裁三个 Unicode 空白字符:蒙古语元音分隔符(U + 180E)、窄的无中断空格(U + 202F)和中等数学空间(U + 205F)。In addition, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions does not trim three Unicode white-space characters: MONGOLIAN VOWEL SEPARATOR (U+180E), NARROW NO-BREAK SPACE (U+202F), and MEDIUM MATHEMATICAL SPACE (U+205F).

另请参阅

Trim(Char)

从当前字符串删除字符的所有前导实例和尾随实例。Removes all leading and trailing instances of a character from the current string.

public:
 System::String ^ Trim(char trimChar);
public string Trim (char trimChar);
member this.Trim : char -> string
Public Function Trim (trimChar As Char) As String

参数

trimChar
Char

要删除的 Unicode 字符。A Unicode character to remove.

返回

从当前字符串的开头和结尾删除 trimChar 字符的所有实例后剩余的字符串。The string that remains after all instances of the trimChar character are removed from the start and end of the current string. 如果从当前实例无法删除字符,此方法返回未更改的当前实例。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.

注解

Trim(System.Char) 方法从当前字符串中移除 trimChar 字符的所有前导和尾随实例。The Trim(System.Char) method removes from the current string all leading and trailing instances of the trimChar character. 当遇到与 trimChar 不同的字符时,将停止每个前导和尾随剪裁操作。Each leading and trailing trim operation stops when a character different from trimChar is encountered. 例如,如果 trimChar - 并且当前字符串为 "---abc---xyz----",则 Trim(System.Char) 方法返回 "abc---xyz"。For example, if trimChar is - and the current string is "---abc---xyz----", the Trim(System.Char) method returns "abc---xyz". 若要删除字符串中的单词之间的字符,请使用.Net 正则表达式To remove characters between words in a string, use .NET Regular Expressions.

备注

如果 Trim(System.Char) 方法从当前实例中删除任何字符,则此方法不会修改当前实例的值。If the Trim(System.Char) method removes any characters from the current instance, this method does not modify the value of the current instance. 相反,它会返回一个新字符串,其中将删除当前实例中找到的所有前导和尾随 trimChar 字符。Instead, it returns a new string in which all leading and trailing trimChar characters found in the current instance are removed.

如果当前字符串等于 Empty 或当前实例中的所有字符都包含 trimChar 字符,则该方法返回 EmptyIf the current string equals Empty or all the characters in the current instance consist of trimChar characters, the method returns Empty.

Trim()

从当前字符串删除所有前导空白字符和尾随空白字符。Removes all leading and trailing white-space characters from the current string.

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

返回

从当前字符串的开头和结尾删除所有空白字符后剩余的字符串。The string that remains after all white-space characters are removed from the start and end of the current string. 如果从当前实例无法删除字符,此方法返回未更改的当前实例。If no characters can be trimmed from the current instance, the method returns the current instance unchanged.

示例

下面的示例使用 String.Trim() 方法从用户输入的字符串中删除任何多余的空格,然后再将它们连接在一起。The following example uses the String.Trim() method to remove any extra white space from strings entered by the user before concatenating them.

using namespace System;

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 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.
    }
}
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 方法从当前字符串中删除所有前导和尾随空白字符。The Trim method removes from the current string all leading and trailing white-space characters. 遇到非空白字符时,每个前导和尾随修整操作都会停止。Each leading and trailing trim operation stops when a non-white-space character is encountered. 例如,如果当前字符串为 "abc xyz",则 Trim 方法返回 "abc xyz"。For example, if the current string is " abc xyz ", the Trim method returns "abc xyz". 若要删除字符串中的单词之间的空白字符,请使用.Net 正则表达式To remove white-space characters between words in a string, use .NET Regular Expressions.

备注

如果 Trim 方法从当前实例中删除任何字符,则此方法不会修改当前实例的值。If the Trim method removes any characters from the current instance, this method does not modify the value of the current instance. 相反,它会返回一个新字符串,其中将删除当前实例中找到的所有前导和尾随空格字符。Instead, it returns a new string in which all leading and trailing white space characters found in the current instance are removed.

如果当前字符串等于 Empty 或当前实例中的所有字符都包含空白字符,则该方法将返回 EmptyIf the current string equals Empty or all the characters in the current instance consist of white-space characters, the method returns Empty.

空白字符由 Unicode 标准定义。White-space characters are defined by the Unicode standard. Trim 方法将删除在将其传递到 Char.IsWhiteSpace 方法时生成 true 的返回值的所有前导和尾随字符。The Trim method removes any leading and trailing characters that produce a return value of true when they are passed to the Char.IsWhiteSpace method.

调用方说明

.NET Framework 3.5 SP1.NET Framework 3.5 SP1 及更早版本维护此方法修整的空白字符的内部列表。The .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions maintain an internal list of white-space characters that this method trims. .NET Framework 4.NET Framework 4开始,方法会剪裁所有 Unicode 空白字符(也就是说,在将其传递到 IsWhiteSpace(Char) 方法时生成 true 返回值的字符)。Starting with the .NET Framework 4.NET Framework 4, the method trims all Unicode white-space characters (that is, characters that produce a true return value when they are passed to the IsWhiteSpace(Char) method). 由于此更改,.NET Framework 3.5 SP1.NET Framework 3.5 SP1 和更早版本中的 Trim() 方法将删除两个字符:零宽度空间(U + 200B)和零宽度无间断空格(U + FEFF),.NET Framework 4.NET Framework 4和更高版本中的 Trim() 方法不会删除。Because of this change, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions removes two characters, ZERO WIDTH SPACE (U+200B) and ZERO WIDTH NO-BREAK SPACE (U+FEFF), that the Trim() method in the .NET Framework 4.NET Framework 4and later versions does not remove. 此外,.NET Framework 3.5 SP1.NET Framework 3.5 SP1 及更早版本中的 Trim() 方法不会剪裁三个 Unicode 空白字符:蒙古语元音分隔符(U + 180E)、窄的无中断空格(U + 202F)和中等数学空间(U + 205F)。In addition, the Trim() method in the .NET Framework 3.5 SP1.NET Framework 3.5 SP1 and earlier versions does not trim three Unicode white-space characters: MONGOLIAN VOWEL SEPARATOR (U+180E), NARROW NO-BREAK SPACE (U+202F), and MEDIUM MATHEMATICAL SPACE (U+205F).

另请参阅

适用于