android string.trim,String.Trim 方法 (System) | Microsoft Docs

移除目前字串開頭和結尾的所有空白字元。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 或目前實例中的所有字元都包含空白字元,則此方法會傳回 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. Trim true 當傳遞給方法時,方法會移除任何會產生傳回值的開頭和結尾字元 Char.IsWhiteSpace 。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 和較舊版本會維護這個方法所修剪的空白字元的內部清單。The .NET Framework 3.5 SP1 and earlier versions maintain an internal list of white-space characters that this method trims. 從 .NET Framework 4 開始,此方法會修剪所有 Unicode 空白字元 (也就是在 true 傳遞至方法) 時產生傳回值的字元 IsWhiteSpace(Char) 。Starting with the .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 和較舊版本中的方法會移除兩個字元,零寬度空間 (U + 200B) 和零寬度的無中斷空間 (U + FEFF) , Trim() .NET Framework 4and 較新版本中的方法不會移除。Because of this change, the Trim() method in the .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 4and later versions does not remove. 此外, Trim() .NET Framework 3.5 SP1 和較舊版本中的方法不會修剪三個 Unicode 空白字元:蒙古文母音分隔符號 (u + 180E) 、窄無空格 (U + 202F) ,以及中數學空間 (u + 205F) 。In addition, the Trim() method in the .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).

另請參閱

適用於

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值