移除目前字串開頭和結尾的所有空白字元。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).

另請參閱

適用於

android string.trim,String.Trim 方法 (System) | Microsoft Docs相关推荐

  1. java enum.isdefined_Enum.IsDefined 方法 (System) | Microsoft Docs

    返回一个布尔值,该值指示给定的整数值或其名称字符串是否存在于指定的枚举中.Returns a Boolean telling whether a given integral value, or it ...

  2. Java String trim()方法示例

    Java String trim() method is used to remove leading and trailing whitespaces from a string. This met ...

  3. 用java自己实现String类的trim()方法功能

    用java自己实现String类的trim()方法功能 我们都知道String类中的trim()方法的功能在于:"返回字符串的副本,其中该副本忽略前导空白和尾部空白."(这是api ...

  4. 在JavaScript中使用Trim String方法

    It's always helpful to have an easy method method on strings to remove trailing or leading whitespac ...

  5. LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。...

    var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new{MatNR = o.MatNR,Ma ...

  6. .net framework4.6项目的dll升级后,未找到方法“System.String.GetPathsOfAllDirectoriesAbove”解决

    .net framework4.6项目的dll升级后,未找到方法"System.String.GetPathsOfAllDirectoriesAbove"解决 参考文章: (1). ...

  7. android中webview loadUrl(String url,Map header)方法和postUrl(String url,byte[] postData)方法同时使用问题;...

    首先说明 loadUrl(String url,Map header)是用于加载webview中添加请求头的方法 postUrl(String url,byte[] postData)是用于加载web ...

  8. LINQ to Entities 不识别方法“System.String ToString() 的解决方法

    今天在做一个页面的时候出现了LINQ to Entities 不识别方法"System.String ToString()"的错误,对于源码IQueryable<Select ...

  9. 关于JAVA的String类的一些方法

    一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str="This is a String"; int len =str.le ...

最新文章

  1. javascript之prototype总结常用方法
  2. Ignoring unused library classes...java.io.IOException: You have to specify '-keep' options for the s
  3. OpenCV 计算物体的凸包
  4. 计算机网络(谢希仁第八版)第一章:概述
  5. xml配置文件的形式 VS 配置类的形式
  6. 做“是非题”的正确姿势
  7. 【SAP HANA】关于SAP HANA中带层次结构的Analytic View创建、激活状况下在系统中生成对象的研究...
  8. SQL Server中TEXT类型操作
  9. java 怎么从date取得年份
  10. 机器学习基础算法12-回归实例-广告预测
  11. java oracle11g jar_oracle11g驱动jar包下载
  12. 电影海报的多标签分类
  13. 8位处理器、16位处理器、32位处理器和64位处理器
  14. Google设置新标签页默认地址
  15. @property详细解读
  16. redis数据库正确用法
  17. 奈学教育大数据架构分享下载
  18. Linux7.0下UNbound搭建DNS服务器
  19. Python自动覆盖录屏软件——NIRE工作室开源软件(参数自动校正)(欢迎白嫖)
  20. Google Earth Engine——无人机影像进行分类处理

热门文章

  1. PIXEL 2XL刷机指南(Android11更新)
  2. 如何恢复删除的照片和视频?可以试试看
  3. 【算法千题案例】每日LeetCode打卡——95.唯一摩尔斯密码词
  4. 浏览器缓存(强缓存和协商缓存)
  5. Substance Painter学习笔记
  6. 程序崩溃-防止第三方库-VDM
  7. KDD-cup 2019比赛总结
  8. Mac 下显示隐藏文件
  9. LayaBox -- 使用滤镜实现图片黑白效果
  10. matlab多行一列扩展多行多列,如何把多列扩展成多行