C# String.TrimStart() – Examples

String.TrimStart() method is used to removes all the leading white-space characters from the current string.

String.TrimStart(Char) method is used to remove specified character from leading edge of the current string.

String.TrimStart(Char[]) method is used to remove all specified characters from leading edge of the current string.

String.TrimStart() does not modify the original string but returns a new String instance with the leading white-space/specified characters from the given string value.

In this tutorial, we will learn about the syntax of C# String.TrimStart(), C# String.TrimStart(Char) and C# String.TrimStart(Char[]) method, and learn how to use these method with the help of examples.

TrimStart()

String.TrimStart() removes all the leading white-space characters from the current string.

ADVERTISEMENT

Syntax

The syntax of TrimStart() method is

String.TrimStart()

Return Value

This method returns String value.

Example 1 – TrimStart()

In this example, we will take a string str with leading white spaces. To remove these leading white spaces, we will call TrimStart() method on this string str. TrimStart() returns a new String instance.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = "  abc  ";
        String result = str.TrimStart();
        Console.WriteLine($"Original String    : \"{str}\"");
        Console.WriteLine($"TrimStart() Result : \"{result}\"");
    }
}

Output

Original String    : "  abc  "
TrimStart() Result : "abc  "

Example 2 – TrimStart() – No Leading Spaces

In this example, we will take a string str with no leading white spaces. If we call TrimStart() method on this string str, TrimStart() returns the original String instance on which we are calling the method. In this case alone, TrimStart() does not create a new String.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = "abc  ";
        String result = str.TrimStart();
        Console.WriteLine($"Original String    : \"{str}\"");
        Console.WriteLine($"TrimStart() Result : \"{result}\"");
    }
}

Output

Original String    : "abc  "
TrimStart() Result : "abc  "

Example 3 – TrimStart() – Null String

In we call TrimStart() method on a null String, then this method throws System.NullReferenceException.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = null;
        String result = str.TrimStart();
        Console.WriteLine($"Original String    : \"{str}\"");
        Console.WriteLine($"TrimStart() Result : \"{result}\"");
    }
}

Output

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Example.Main(String[] args) in D:\workspace\csharp\HelloWorld\Program.cs:line 6

TrimStart(Char)

String.TrimStart() removes all the leading occurrences of a specified character from the current string.

Syntax

The syntax of TrimStart(Char) method is

String.TrimStart(Char trimChar)

where

ParameterDescription
trimCharA Unicode character to remove from leading edge of the String.

Return Value

This method returns String value.

Example – TrimStart(Char)

In this example, we will take a string, and trim the specified character if present at the leading edge of this string using TrimStart(Char) method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = "aaabcdeaaa";
        Char trimChar = 'a';
        String result = str.TrimStart(trimChar);
        Console.WriteLine($"Original String    : \"{str}\"");
        Console.WriteLine($"TrimStart() Result : \"{result}\"");
    }
}

Output

Original String    : "aaabcdeaaa"
TrimStart() Result : "bcdeaaa"

Only the leading edge is trimmed, not the trialing edge.

TrimStart(Char[])

String.TrimStart() removes all the leading occurrences of a set of characters specified in an array from the current string.

Syntax

The syntax of TrimStart(Char[]) method is

String.TrimStart(Char[] trimChars)

where

ParameterDescription
trimCharsThe array of Unicode characters to remove from trailing edge of given string.

Return Value

This method returns String value.

Example 5 – TrimStart(Char[])

In this example, we will take a string and remove all the leading characters that are in the specified character array trimChars, using TrimStart(Char[]) method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = "aaabcdababa";
        Char[] trimChars = {'a', 'b'};
        String result = str.TrimStart(trimChars);
        Console.WriteLine($"Original String    : \"{str}\"");
        Console.WriteLine($"TrimStart() Result : \"{result}\"");
    }
}

Output

Original String    : "aaabcdababa"
TrimStart() Result : "cdababa"

Conclusion

In this C# Tutorial, we have learnt the syntax of C# String.TrimStart() method, and also learnt how to use this method with the help of C# example programs.