String.Trim 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回新字串,在此移除所有出現在目前字串開頭和結尾的指定字元集。
多載
Trim() |
移除目前字串開頭和結尾的所有空白字元。 |
Trim(Char[]) |
移除出現在目前字串開頭和結尾的所有陣列指定字元集。 |
Trim(Char) |
移除目前字串字元開頭和結尾的所有字元執行個體。 |
Trim()
移除目前字串開頭和結尾的所有空白字元。
public:
System::String ^ Trim();
public string Trim ();
member this.Trim : unit -> string
Public Function Trim () As String
傳回
從目前字串的開頭和結尾移除所有空白字元後,所保留下來的字串。 如果在目前的執行個體中無法修剪任何字元,則方法傳回未變更的目前執行個體。
範例
下列範例會 String.Trim() 使用 方法,在串連使用者之前,先從使用者輸入的字串中移除任何額外的空白字元。
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.
}
}
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) 和零寬度 NO-BREAK SPACE (U+FEFF) , Trim() .NET Framework 4 和更新版本中的方法不會移除。 此外, Trim() .NET Framework 3.5 SP1 和較舊版本中的 方法不會修剪三個 Unicode 空白字元:在 U+180E (U+180E) 、NARROW NO-BREAK SPACE (U+202F) ,以及中數學空間 (U+205F) 。
另請參閱
適用於
Trim(Char[])
移除出現在目前字串開頭和結尾的所有陣列指定字元集。
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
參數
- trimChars
- Char[]
要移除的 Unicode 字元陣列或 null
。
傳回
從目前的字串開頭和結尾處移除 trimChars
參數中所有出現的字元後,所保留下來的字串。 如果 trimChars
是 null
或空陣列,則反而會移除空白字元。 如果在目前的執行個體中無法修剪任何字元,則方法傳回未變更的目前執行個體。
範例
下列範例會 Trim(System.Char[])
使用 方法來移除空格、星號 (*) ,以及字串中的單引號 () 字元。
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("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 回 。
如果 為 null
trimChars
或空陣列,這個方法會移除任何前置或尾端字元,以在方法傳遞至 Char.IsWhiteSpace 方法時傳回 true
方法。
給呼叫者的注意事項
.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 SPACE (U+FEFF) , Trim() .NET Framework 4 和更新版本中的方法不會移除。 此外, Trim() .NET Framework 3.5 SP1 和較舊版本中的 方法不會修剪三個 Unicode 空白字元:在 U+180E (U+180E) 、NARROW NO-BREAK SPACE (U+202F) ,以及中數學空間 (U+205F) 。
另請參閱
適用於
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
要移除的 Unicode 字元。
傳回
從目前字串的開頭和結尾移除所有 trimChar
字元執行個體後,遺留下來的字串。 如果在目前的執行個體中無法修剪任何字元,則方法傳回未變更的目前執行個體。
備註
方法 Trim(System.Char)
會從目前字串中移除字元的所有前置和尾端實例 trimChar
。 遇到不同于 trimChar
的字元時,每個前置和尾端修剪作業都會停止。 例如,如果 trimChar
is -
且目前的字串為 「---abc---xyz----」,則 Trim(System.Char)
方法會傳回 「abc---xyz」。 若要移除字串中單字之間的字元,請使用 .NET 正則運算式。
注意
Trim(System.Char)
如果方法從目前實例移除任何字元,這個方法就不會修改目前實例的值。 相反地,它會傳回新的字串,其中會移除目前實例中找到的所有前置和尾端 trimChar
字元。
如果目前的字串等於 Empty 或目前實例中的所有字元包含 trimChar
字元,則 方法會傳 Empty 回 。