Export (0) Print
Expand All
2 out of 7 rated this helpful - Rate this topic

String.TrimStart Method

Removes all occurrences of a set of characters specified in an array from the beginning of this instance.

[Visual Basic]
Public Function TrimStart( _
   ByVal ParamArray trimChars() As Char _
) As String
[C#]
public string TrimStart(
   params char[] trimChars
);
[C++]
public: String* TrimStart(
 __wchar_t trimChars __gc[]
);
[JScript]
public function TrimStart(
   trimChars : Char[]
) : String;

Parameters

trimChars
An array of Unicode characters to be removed or a null reference (Nothing in Visual Basic).

Return Value

The String that remains after all occurrences of characters in trimChars are removed from the beginning. If trimChars is a null reference (Nothing in Visual Basic), white space characters are removed instead.

Remarks

For more information about what Unicode characters are categorized as white space characters, see the Remarks section of the Char.IsWhiteSpace method.

Example

[Visual Basic, C#, C++] The following code example demonstrates how you can use the TrimStart method to trim white space or other characters from the beginning of a string.

[Visual Basic] 
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

[C#] 
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;
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

String* MakeArray()[]
{
   String* arr[] = {S"  please    ", S"  tell    ", S"  me    ", S"  about    ", S"  yourself    "};
   return arr;
}

int main()
{

   String* temp[] = MakeArray();

   Console::WriteLine(S"Concatenating the inital values in the array, we get the string:");
   Console::WriteLine(S"'{0}'\n", String::Concat(temp));

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

   Console::WriteLine(S"Concatenating the trimmed values in the array, we get the string:");
   Console::WriteLine(S"'{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->Item[i] = temp[i]->TrimStart(0);

   Console::WriteLine(S"Concatenating the start-trimmed values in the array, we get the string:");
   Console::WriteLine(S"'{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->Item[i] = temp[i]->TrimEnd(0);

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | Char | Trim | TrimEnd

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.