Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# Resources


The C# string 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 below example, TrimStart() method to used to return 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 below example, TrimStart() method to used to return 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