Summary: in this tutorial, you will learn how to use the C# String Trim() method to remove the leading and trailing white spaces from a string.
Introduction to the C# String Trim() method
The String Trim() method allows you to remove the leading and trailing white spaces from a string. It returns a new string with the leading and trailing white spaces removed.
Here’s the syntax of the Trim() method:
public string Trim();Code language: C# (cs)The following example shows how to use the Trim() method to remove the leading and trailing white spaces from the string " Hello, World! ";
using static System.Console;
var message = " Hello, World! ";
var trimmed = message.Trim();
WriteLine(message);
WriteLine(trimmed);Code language: C# (cs)Output:
Hello, World!
Hello, World!Code language: C# (cs)In this example, the message string contains leading and trailing white spaces. We call the Trim() method on the message string. The method returns a result string (trimmed) that has the leading and trailing white spaces removed.
Note that the Trim() method doesn’t change the original string but returns a new string.
The Unicode standard defines white-space characters. The Trim() method removes any leading and trailing characters that, when passed to the Char.IsWhiteSpace() method, returns true:
public static bool IsWhiteSpace (char c);Code language: C# (cs)Using C# String Trim() to remove the leading and trailing instances of a character
The String Trim() method has an overload that allows you to remove the leading and trailing instances of a character from the current string:
public string Trim (char trimChar);Code language: C# (cs)In this syntax, the trimChar is a character to remove. The Trim() method returns a new string with the leading and trailing trimChar removed.
For example, the following program uses the Trim() method to remove the leading and trailing asterisk (*) from a string:
using static System.Console;
var message = "***Hello, World!***";
var trimmed = message.Trim('*');
WriteLine(message);
WriteLine(trimmed);Code language: C# (cs)Output:
***Hello, World!***
Hello, World!Code language: C# (cs)Using C# String Trim() to remove the leading and trailing occurrences of a set of characters
The String Trim() method has an overload that removes any leading and trailing instances of specified characters from the current string:
public string Trim (
params char[]? trimChars
);Code language: C# (cs)In this syntax, the trimChars is an array of characters to remove. Note that if the trimChars is null, the Trim() method returns a new string with the leading and trailing white spaces removed.
The following example uses the Trim() method to remove the leading and trailing characters * and spaces from a string:
using static System.Console;
var message = "*** Hello, World! ***";
char[] trimChars = { '*', ' '};
var trimmed = message.Trim(trimChars);
WriteLine(message);
WriteLine(trimmed);Code language: C# (cs)Output:
*** Hello, World! ***
Hello, World!Code language: C# (cs)Summary
- Use the C# String
Trim()method to remove the leading and trailing white spaces or a set of specified characters from a string.