Trim String in C#

To trim a string in C#, we can use String.Trim method that 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. Code example of string.trim is below.
 
In .NET and 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.



Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.