In C#, TrimEnd() removes characters from the end of a string only. With no arguments it strips trailing white space, including line breaks. With arguments it strips the characters you list.
Typical uses are removing a trailing slash from a URL, extra zeros after a decimal point, or a \r\n left at the end of a line you read. The start and the middle are left alone, and a new string is returned.
This is an instance member of System.String. Call it on a string variable, such as text.TrimEnd(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: TrimEnd() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.
Syntax
Call it with no arguments for white space, or pass the characters to strip from the end.
C#
string a = "line ".TrimEnd(); // "line"
string b = "Hello!!!".TrimEnd('!'); // "Hello"
string c = "data\r\n".TrimEnd('\r', '\n'); // "data"
Call
What it does
text.TrimEnd()
Remove trailing white space and line breaks.
url.TrimEnd('/')
Remove trailing slashes.
line.TrimEnd('\r', '\n')
Remove only line-break characters at the end.
Trimming runs from the last character backward and stops at the first character that is not in the set. If all characters match, you get "". Newer .NET adds a single-char overload and a dedicated no-argument overload; the params char[] form works everywhere.
Library declarations
C#
public string TrimEnd(); // newer .NET
public string TrimEnd(char trimChar); // newer .NET
public string TrimEnd(params char[]? trimChars);
Parameters
Parameter
Description
trimChars / trimChar
Optional. The characters to remove from the end. Empty or left out means white space.
Return Value
Return Value
Description
string
A new string without the trailing characters, or the same text when nothing needed trimming.
C# Examples
Example 1 — Clean the End and Keep the Front
Remove trailing spaces and show that leading spaces are kept.
C#
using System;
class Program {
static void Main() {
Console.WriteLine("[" + "line ".TrimEnd() + "]");
Console.WriteLine("[" + " a ".TrimEnd() + "]");
}
}
Output
[line]
[ a]
How It Works
Trailing spaces go. The three spaces after line are removed.
Leading spaces stay. In " a " only the end is cleaned, so the two spaces after [ remain.
The mirror of TrimStart. Same rules, opposite side.
Good for indented text. When indentation matters, such as code or poems, TrimEnd cleans line ends without touching the indent.
Example 2 — Remove Trailing Punctuation and Zeros
Strip repeated exclamation marks and the extra zeros after a decimal point, then remove a dot that is left alone.
C#
using System;
class Program {
static void Main() {
Console.WriteLine("Hello!!!".TrimEnd('!'));
Console.WriteLine("3.1400".TrimEnd('0'));
Console.WriteLine("5.000".TrimEnd('0').TrimEnd('.'));
}
}
Output
Hello
3.14
5
How It Works
Repeats are removed together. All three ! marks are trailing, so they all go.
Zeros after the last digit.3.1400 loses its two trailing zeros. The zero-free part 3.14 is kept.
A dot left alone.5.000 loses all its zeros and becomes 5.; a second TrimEnd('.') removes the dot.
Text only. This trims digits as characters. For real numbers, formatting with ToString("0.##") is usually clearer.
Example 3 — Normalize a URL and a Line Read from a File
Remove any number of trailing slashes, and strip a Windows line break from the end of a line.
C#
using System;
class Program {
static void Main() {
string url = "https://site.com///";
Console.WriteLine(url.TrimEnd('/'));
string line = "data\r\n";
Console.WriteLine("[" + line.TrimEnd('\r', '\n') + "]");
Console.WriteLine(line.Length);
}
}
Output
https://site.com
[data]
6
How It Works
Any number of slashes. The three trailing slashes are all removed in one call. The slashes in https:// are not at the end, so they stay.
Only line-break characters. Listing '\r' and '\n' removes the Windows line ending but would keep a trailing space or tab.
Visible edges. The brackets show that nothing is left after data.
Original length.line still has 6 characters (4 letters plus 2 break characters), because trimming made a new string.
Frequently Asked Questions
It returns a new string with white space, or the characters you pass, removed from the end only.
Use url.TrimEnd('/'). It removes all trailing slashes, not just one.
Use line.TrimEnd('\r', '\n') to remove only line breaks, or TrimEnd() to remove all trailing white space.
No. It removes any characters from the set, one by one. To remove an exact suffix once, test it with EndsWith and then cut it with Substring.
No. Strings are immutable, so TrimEnd returns a new string. Assign it if you want to keep it.