C# String - TrimStart() Method



The C# String TrimStart() method is used to return a string after the trimming all the leading white space characters or (specified character) from a string. This method does not modified the original string but returns a new string with the changes.

Syntax

Following are the syntax of the C# string TrimStart() method −

Default Syntax −

This syntax of the TrimStart method removes all leading white-space characters from the current string.

public string TrimStart();

Parametrised Syntax −

This syntax of the TrimStart method removes all leading occurrences of the specified characters from the string.

public string TrimStart(params char[]? trimChars);

Parameters

This method accepts the following parameters −

  • trimChars: This is an array of Unicode characters to remove, or null.

Return Value

This method returns a string that remains after all occurrences of the characters in the trimChars parameter are removed from the beginning of the current string.

Example 1: Removing Leading WhiteSpace

Following is a basic example of the TrimStart() method to remove leading white space characters from the current string −

    
using System;
class Program {
   static void Main() {
      string str = "   Hii, tutorialspoint!   ";
      string trimmedStr = str.TrimStart();

      Console.WriteLine($"Original: '{str}'");
      Console.WriteLine($"TrimmedString: '{trimmedStr}'");
   }
}

Output

Following is the output −

Original: '   Hii, tutorialspoint!   '
TrimmedString: 'Hii, tutorialspoint!   '

Example 2: Trim Specific Leading Characters

Let's look at another example. Here, we use the parametrized TrimStart() method to remove the specified special character from start of the string −

using System;
class Program {
   static void Main() {
      string str = "!!Hello, tutorialspoint!!";
      string trimmedStr = str.TrimStart('!', ',');
      
      Console.WriteLine($"Original: '{str}'");
      Console.WriteLine($"TrimmedString: '{trimmedStr}'");
   }
}

Output

Following is the output −

Original: '!!Hello, tutorialspoint!!'
TrimmedString: 'Hello, tutorialspoint!!'

Example 3: Sanitizing User Input

In this example, we use the TrimStart() method to sanitize user input by removing or modifying unsafe characters from starting of the user input −

using System;
class Program {
   public static void Main() {
      string userInput = "  admin";
      if (userInput.TrimStart() == "admin") {
         Console.WriteLine("Valid input");
      }
      else{
         Console.WriteLine("Invalid Input");
      }
   }
}

Output

Following is the output −

Valid input

Example 4: Check for WhiteSpace or Specified Char

The example below checks if there is whitespace or specified characters at the beginning of the string before trimming it −

using System;
class Program {
   static void Main() {
      string str = "   !!tutorialspoit, Hello World";
      
      // Check for whitespace
      if (str.Contains(' ')) {
         Console.WriteLine("The string contains whitespace.");
      }
   
      if (str.Contains('!') || str.Contains(',')) {
         Console.WriteLine("The string contains '!' or ',' characters.");
      }   
      string trimmedStr = str.TrimStart('!', ' ');   
      Console.WriteLine($"Original String: '{str}'");
      Console.WriteLine($"TrimStartmed String: '{trimmedStr}'");
   }
}

Output

Following is the output −

The string contains whitespace.
The string contains '!' or ',' characters.
Original String: '   !!tutorialspoit, Hello World'
TrimStartmed String: 'tutorialspoit, Hello World'
csharp_strings.htm
Advertisements