Trim String in C#

Introduction

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. The 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. 

// String with whitespaces  
string hello = " hello C# Corner has white spaces ";  
// Remove whitespaces from both ends  
Console.WriteLine(hello.Trim());  
  
// String with characters  
string someString = ".....My name is Mahesh Chand****";  
char[] charsToTrim = { '*', '.' };  
string cleanString = someString.Trim(charsToTrim);  
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. 

// String with whitespaces  
string hello = " hello C# Corner has white spaces ";  
// Remove whitespaces from both ends  
Console.WriteLine(hello.TrimStart());  
  
// String with characters  
string someString = ".....My name is Mahesh Chand****";  
char[] charsToTrim = { '*', '.' };  
string cleanString = someString.TrimStart(charsToTrim);  
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. 

// String with whitespaces  
string hello = " hello C# Corner has white spaces ";  
// Remove whitespaces from both ends  
Console.WriteLine(hello.TrimEnd());  
  
// String with characters  
string someString = ".....My name is Mahesh Chand****";  
char[] charsToTrim = { '*', '.' };  
string cleanString = someString.TrimEnd(charsToTrim);  
Console.WriteLine(cleanString);  

Listing 3.

The output of Listing 3 looks like Figure 3.

C# TrimEnd 

Figure 3.

Conclusion

In this article, we learned about String.Trim() method 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. 


Similar Articles
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.