C# Tutorial C# Advanced C# References

C# String - TrimStart() Method



The C# TrimStart() method is used to removes all leading instances of a character(s) / whitespaces from the current string.

Note: This method can be overloaded by passing different type of arguments to it.

Syntax

//removes whitespaces
public string TrimStart();

//removes specified character
public string TrimStart(char trimChar);

//removes characters specified in the array
public string TrimStart(params char[] trimChars);

Parameters

trimChar Specify the character to remove.
trimChars Specify the array of characters to remove.

Return Value

Returns the start side trimmed version of the string.

Exception

NA.

Example:

In the example below, TrimStart() method returns the start side trimmed version of the string MyStr by removing whitespaces.

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyStr = "    Hello World!.    ";
    string NewStr = MyStr.TrimStart();

    Console.WriteLine(NewStr); 
  }
}

The output of the above code will be:

Hello World!.    

Example:

In the example below, TrimStart() method returns the start side trimmed version of the string MyStr by removing the specified characters in the char array.

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyStr = ",*#Hello World!#@!";
    char[] CharToTrim = {',','*','#','@','!'};
    string NewStr = MyStr.TrimStart(CharToTrim);

    Console.WriteLine(NewStr); 
  }
}

The output of the above code will be:

Hello World!#@!

❮ C# String Methods