String.TrimStart 方法

从此实例的开始位置移除数组中指定的一组字符的所有匹配项。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Function TrimStart ( _
    ParamArray trimChars As Char() _
) As String
用法
Dim instance As String
Dim trimChars As Char()
Dim returnValue As String

returnValue = instance.TrimStart(trimChars)
public string TrimStart (
    params char[] trimChars
)
public:
String^ TrimStart (
    ... array<wchar_t>^ trimChars
)
public String TrimStart (
    char[] trimChars
)
public function TrimStart (
    ... trimChars : char[]
) : String

参数

  • trimChars
    要移除的 Unicode 字符数组或 空引用(在 Visual Basic 中为 Nothing)。

返回值

从开始位置移除 trimChars 中字符的所有匹配项后剩余的 String。如果 trimChars 为 空引用(在 Visual Basic 中为 Nothing),则改为移除空白字符。

备注

有关将哪些 Unicode 字符归类为空白字符的更多信息,请参见 Trim 方法重载的“备注”部分。

示例

下面的代码示例演示如何使用 TrimStart 方法重载删除字符串开始处的空白字符或其他字符。

Imports System

Public Class TrimTest
    
    Public Shared Sub Main()
        Dim temp As String() = MakeArray()
        
        
        Console.WriteLine("Concatenating the inital values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
        Dim i As Integer
        
        ' trim whitespace from both ends of the elements
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).Trim()
        
        Next i
        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
        
        ' reset the array
        temp = MakeArray()
        
        ' trim the start of the elements. Passing null trims whitespace only
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).TrimStart(" "c)
        Next i

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)

        ' reset the array
        temp = MakeArray()

        ' trim the end of the elements. Passing null trims whitespace only
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).TrimEnd(" "c)
        Next i

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'", [String].Concat(temp))
    End Sub 'Main

    Private Shared Function MakeArray() As String()
        Dim arr As String() = {"  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "}
        Return arr
    End Function 'MakeArray
End Class 'TrimTest
using System;

public class TrimTest {
    public static void Main() {

        string [] temp = MakeArray();

        Console.WriteLine("Concatenating the inital values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // trim whitespace from both ends of the elements
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].Trim();

        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the start of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimStart(null);

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the end of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimEnd(null);

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'", String.Concat(temp));
    }

    private static string [] MakeArray() {
        string [] arr = {"  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "};
        return arr;
    }
}
using namespace System;

array<String^>^ MakeArray()
{
   array<String^>^arr = {"  please    ","  tell    ","  me    ","  about    ","  yourself    "};
   return arr;
}

int main()
{
   array<String^>^temp = MakeArray();
   Console::WriteLine( "Concatenating the inital values in the array, we get the string:" );
   Console::WriteLine( "'{0}'\n", String::Concat( temp ) );

   // trim whitespace from both ends of the elements
   for ( int i = 0; i < temp->Length; i++ )
      temp[ i ] = temp[ i ]->Trim();
   Console::WriteLine( "Concatenating the trimmed values in the array, we get the string:" );
   Console::WriteLine( "'{0}'", String::Concat( temp ) );

   // reset the array
   temp = MakeArray();

   // trim the start of the elements-> Passing 0 trims whitespace only
   for ( int i = 0; i < temp->Length; i++ )
      temp[ i ] = temp[ i ]->TrimStart( 0 );
   Console::WriteLine( "Concatenating the start-trimmed values in the array, we get the string:" );
   Console::WriteLine( "'{0}'\n", String::Concat( temp ) );

   // reset the array
   temp = MakeArray();

   // trim the end of the elements-> Passing 0 trims whitespace only
   for ( int i = 0; i < temp->Length; i++ )
      temp[ i ] = temp[ i ]->TrimEnd( 0 );
   Console::WriteLine( "Concatenating the end-trimmed values in the array, we get the string:" );
   Console::WriteLine( "'{0}'", String::Concat( temp ) );
}
import System.*;

public class TrimTest
{
    public static void main(String[] args)
    {
        String temp[] = MakeArray();

        Console.WriteLine("Concatenating the inital values in the array," 
            + " we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp),
            Environment.get_NewLine());
        // trim whitespace from both ends of the elements
        for (int i = 0; i < temp.get_Length(); i++) {
            temp.set_Item(i, temp[i].Trim());
        }
        Console.WriteLine("Concatenating the trimmed values in the array," 
            + " we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), 
            Environment.get_NewLine());
        // reset the array
        temp = MakeArray();
        // trim the start of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.get_Length(); i++) {
            temp.set_Item(i, temp[i].TrimStart(null));
        }
        Console.WriteLine("Concatenating the start-trimmed values in the " 
            + "array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), 
            Environment.get_NewLine());
        // reset the array
        temp = MakeArray();
        // trim the end of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.get_Length(); i++) {
            temp.set_Item(i, temp[i].TrimEnd(null));
        }
        Console.WriteLine("Concatenating the end-trimmed values in the array," 
            + " we get the string:");
        Console.WriteLine("'{0}'", String.Concat(temp));
    } //main

    private static String[] MakeArray()
    {
        String arr[] =  { "  please    ", "  tell    ", "  me    ", 
            "  about    ", "  yourself    " };
        return arr;
    } //MakeArray
} //TrimTest
import System;

public class TrimTest {
    public static function Main() : void {

        var temp : String [] = MakeArray();

        Console.WriteLine("Concatenating the inital values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // trim whitespace from both ends of the elements
        for (var i : int = 0; i < temp.Length; i++)
            temp[i] = temp[i].Trim();

        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

    var c : char[] = undefined;

        // trim the start of the elements. Passing null trims whitespace only
        for (i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimStart(c);

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the end of the elements. Passing null trims whitespace only
        for (i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimEnd(c);

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'", String.Concat(temp));
    }

    private static function MakeArray() : String [] {
        var arr : String [] = ["  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "];
        return arr;
    }
}
TrimTest.Main();

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

String 类
String 成员
System 命名空间
Char 结构
Trim
TrimEnd