System.String.TrimStart 方法

方法描述

从当前 String 对象移除数组中指定的一组字符的所有前导匹配项。

语法定义(C# System.String.TrimStart 方法 的用法)

public string TrimStart(
	params char[] trimChars
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
trimChars System-Char[] 要删除的 Unicode 字符的数组,或 null。
返回值 System.String 从当前字符串的开头删除所出现的所有 trimChars 参数中的字符后剩余的字符串。 如果 trimChars 为 null 或空数组,则改为删除空白字符。

提示和注释

TrimStart 方法从当前字符串移除 trimChars 参数中的所有前导字符。 遇到不在 trimChars 中的字符时,裁剪操作停止。 例如,如果当前字符串为“123abc456xyz789”并且 trimChars 包含从“1”到“9”的数字,则 TrimStart 方法返回“abc456xyz789”。

注意

此方法不修改当前实例的值。 而是返回一个新字符串,在该字符串中,移除了当前实例中找到的所有前导空白字符。

对调用者的说明

如果 trimChars 为 null 或空数组,.NET Framework 3.5 SP1 和早期版本维护一个此方法修整的空白字符的内部列表。 从 .NET Framework 4 开始,如果 trimChars 为 null 或空数组,该方法将修整所有 Unicode 空白字符(也就是在传递到 Char.IsWhiteSpace 方法时生成 true 返回值的字符)。 由于该更改,.NET Framework 3.5 SP1 中的 Trim() 方法以及早期版本会删除两个字符 ZERO WIDTH SPACE (U+200B) 和 ZERO WIDTH NO-BREAK SPACE (U+FEFF),而 .NET Framework 4 中的 Trim() 方法不会删除这些字符。 此外,.NET Framework 3.5 SP1 以及更早版本中的 Trim() 方法也不会修整三个 Unicode 空白字符:MONGOLIAN VOWEL SEPARATOR (U+180E)、NARROW NO-BREAK SPACE (U+202F) 和 MEDIUM MATHEMATICAL SPACE (U+205F)。

System.String.TrimStart 方法例子

然后,下面的示例演示对 StripComments 方法的调用。

public static void Main()
{
   string[] lines= {"using System;",
                    "", 
                    "public class HelloWorld",
                    "{", 
                    "   public static void Main()",
                    "   {", 
                    "      // This code displays a simple greeting", 
                    "      // to the console.", 
                    "      Console.WriteLine(\"Hello, World.\");", 
                    "   }", 
                    "}"};
   Console.WriteLine("Before call to StripComments:");
   foreach (string line in lines)
      Console.WriteLine("   {0}", line);                         

   string[] strippedLines = StripComments(lines); 
   Console.WriteLine("After call to StripComments:");
   foreach (string line in strippedLines)
      Console.WriteLine("   {0}", line);                         
}
// This code produces the following output to the console:
//    Before call to StripComments:
//       using System;
//   
//       public class HelloWorld
//       {
//           public static void Main()
//           {
//               // This code displays a simple greeting
//               // to the console.
//               Console.WriteLine("Hello, World.");
//           }
//       }  
//    After call to StripComments:
//       This code displays a simple greeting
//       to the console.

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。