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.

如果 trimCharsnull 或空陣列,則這個方法會在傳遞至 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).

另請參閱

適用於