Summary: in this tutorial, you will learn how to use the C# String TrimEnd()
method to remove all the trailing white-space characters from a string.
Introduction to the C# String TrimEnd() method
The String TrimEnd()
method allows you to remove all the trailing white-space characters from a string. It returns a new string with the trailing white spaces removed.
The following shows the syntax of the TrimEnd()
method:
public string TrimEnd();
Code language: C# (cs)
In this example, we use the TrimEnd()
method to remove the trailing white spaces from the string "Hello, World! "
;
using static System.Console;
var message = "Hello, World ";
var result = message.TrimEnd();
Write(result);
Write('!');
Code language: C# (cs)
Output:
Hello, World!
Code language: C# (cs)
In this example, the message
string contains trailing white spaces. We call the TrimEnd()
method on the message
string. The method returns a result string (trimmed
) that has the trailing white spaces removed.
Note that the TrimEnd()
method doesn’t change the original string but returns a new string.
The Unicode standard defines white-space characters. The TrimEnd()
method removes any trailing 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 TrimEnd() to remove the trailing instances of a character
The String TrimEnd()
method has an overload that allows you to remove the trailing instances of a character from the current string:
public string TrimEnd(char trimChar);
Code language: C# (cs)
In this syntax, the trimChar
is a character to remove. The TrimEnd()
method returns a new string with all the trailing trimChar
removed.
For example, the following program uses the TrimEnd()
method to remove the trailing (!
) from a string:
using static System.Console;
var message = "Hello, World!";
var result = message.TrimEnd('!');
WriteLine(message);
WriteLine(result);
Code language: C# (cs)
Output:
Hello, World!
Hello, World
Code language: C# (cs)
Using C# String TrimEnd() to remove the trailing occurrences of a set of characters
The String TrimEnd()
method has an overload that removes any trailing and trailing instances of specified characters from the current string:
public string TrimEnd (
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 TrimEnd()
method returns a new string with the trailing white spaces removed.
The following example uses the TrimEnd()
method to remove the trailing characters !
and spaces from a string:
using static System.Console;
var message = "Hello, World! ***";
char[] trimChars = { '*', '!', ' ' };
var result = message.TrimEnd(trimChars);
WriteLine(result);
Code language: C# (cs)
Output:
Hello, World
Code language: C# (cs)
Summary
- Use the C# String
TrimEnd()
method to remove all the trailing white spaces or a set of specified characters from a string.