Trim String in C#

String.Trim(), String.TrimStart(), and String.TrimEnd() methods provide trim string functionality in C# and .NET. The Trim method removes white spaces from the beginning and endof a string. The TrimEnd method removes characters specified in an array of characters from the end of a string and TrimStart method removes characters specified in an array of characters from the beginning of a string.

The Trim method trims a string from both ends by removing white spaces or a specified character or characters from the start and the end of a string.
 
In C#, Strings are immutable. That means, the method does not update the existing string but returns a new string.
 
String.Trim()
 
String.Trim() method has two overloaded forms. 
 

Method

Description

Trim(Char[])

Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.

Trim()

Removes all leading and trailing white-space characters from the current String object.

 
Example
 
The following example uses Trim methods to trim whitespaces and other characters from a string. 
  1. // String with whitespaces  
  2. string hello = " hello C# Corner has white spaces ";  
  3. // Remove whitespaces from both ends  
  4. Console.WriteLine(hello.Trim());  
  5.   
  6. // String with characters  
  7. string someString = ".....My name is Mahesh Chand****";  
  8. char[] charsToTrim = { '*''.' };  
  9. string cleanString = someString.Trim(charsToTrim);  
  10. Console.WriteLine(cleanString);  
Listing 1.
 
The output of Listing 1 looks like Figure 1.
 
C# Trim String 
 
Figure 1.
 
String.TrimStart()
 
String.TrimStart() method removes all leading occurrences of a set of characters specified in an array from the current String object.
 
Example
 
The following example uses TrimStart methods to trim whitespaces and other characters from the start of a string. 
  1. // String with whitespaces  
  2. string hello = " hello C# Corner has white spaces ";  
  3. // Remove whitespaces from both ends  
  4. Console.WriteLine(hello.TrimStart());  
  5.   
  6. // String with characters  
  7. string someString = ".....My name is Mahesh Chand****";  
  8. char[] charsToTrim = { '*''.' };  
  9. string cleanString = someString.TrimStart(charsToTrim);  
  10. Console.WriteLine(cleanString);  
Listing 2.
 
The output of Listing 2 looks like Figure 2.
 
String.TrimStart() Method 
 
Figure 2.
 
String.TrimEnd()
 
String.TrimEnd() method removes all trailing occurrences of a set of characters specified in an array from the current String object.
 
Example
 
The following example replaces all commas with a colon in a string. 
  1. // String with whitespaces  
  2. string hello = " hello C# Corner has white spaces ";  
  3. // Remove whitespaces from both ends  
  4. Console.WriteLine(hello.TrimEnd());  
  5.   
  6. // String with characters  
  7. string someString = ".....My name is Mahesh Chand****";  
  8. char[] charsToTrim = { '*''.' };  
  9. string cleanString = someString.TrimEnd(charsToTrim);  
  10. Console.WriteLine(cleanString);  
Listing 3.
 
The output of Listing 3 looks like Figure 3.
 
C# TrimEnd 
 
Figure 3.