String.Trim String.Trim String.Trim String.Trim Method

定義

傳回新字串,其中已經移除出現在目前 String 物件中之所有開頭與結尾的一組指定的字元。Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current String object are removed.

多載

Trim(Char[]) Trim(Char[]) Trim(Char[]) Trim(Char[])

從目前的 String 物件中移除陣列中指定之一組字元的所有開頭和結尾指定項目。Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.

Trim(Char) Trim(Char) Trim(Char) Trim(Char)
Trim() Trim() Trim() Trim()

從目前的 String 物件中移除所有的開頭和結尾空白字元。Removes all leading and trailing white-space characters from the current String object.

Trim(Char[]) Trim(Char[]) Trim(Char[]) Trim(Char[])

從目前的 String 物件中移除陣列中指定之一組字元的所有開頭和結尾指定項目。Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.

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.

範例

下列範例會使用String.Trim(Char[])方法,從字串中移除空格、星號(*)和單引號(')字元。The following example uses the String.Trim(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'

備註

方法會從目前的字串trimChars中移除參數中所有開頭和尾端的字元。 TrimThe Trim 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方法會傳回 "abc456xyz"。For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the Trim method returns "abc456xyz".

注意

Trim如果方法從目前的實例移除任何字元,這個方法就不會修改目前實例的值。If the Trim 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 Empty陣列中的字元所組成,則此方法會傳回。If 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 true Char.IsWhiteSpacenull或空陣列,這個方法會在傳遞給方法時,移除任何會導致方法傳回的開頭或尾端字元。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更早的版本會維護此方法在為null或空陣列時trimChars ,所修剪之空白字元的內部清單。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. null IsWhiteSpace(Char) true從開始trimChars ,如果是或空陣列,則方法會修剪所有 Unicode 空白字元(也就是在傳遞至方法時產生傳回值的字元)。 .NET Framework 4.NET Framework 4Starting 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). 由於Trim()這項變更, .NET Framework 3.5 SP1.NET Framework 3.5 SP1和較舊版本中的方法會移除兩個字元:零寬度空格(u + 200B)和零寬度不中斷空間(u + FEFF), .NET Framework 4.NET Framework 4Trim()和更新版本中的方法會執行不移除。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) Trim(Char) Trim(Char) Trim(Char)

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 Char Char Char

傳回

Trim() Trim() Trim() Trim()

從目前的 String 物件中移除所有的開頭和結尾空白字元。Removes all leading and trailing white-space characters from the current String object.

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或目前實例中的所有字元都是由空白字元組成,則Empty方法會傳回。If 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. 方法會在傳遞給Char.IsWhiteSpace方法時,移除任何會產生傳回值true的開頭和尾端字元。 Trim()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. 從開始true IsWhiteSpace(Char) ,方法會修剪所有 Unicode 空白字元(也就是在傳遞至方法時產生傳回值的字元)。 .NET Framework 4.NET Framework 4Starting 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). 由於Trim()這項變更, .NET Framework 3.5 SP1.NET Framework 3.5 SP1和較舊版本中的方法會移除兩個字元:零寬度空格(u + 200B)和零寬度不中斷空間(u + FEFF), .NET Framework 4.NET Framework 4Trim()和更新版本中的方法會執行不移除。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).

另請參閱

適用於