C# String TrimEnd() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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"
CallWhat 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

ParameterDescription
trimChars / trimCharOptional. The characters to remove from the end. Empty or left out means white space.

Return Value

Return ValueDescription
stringA 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() + "]");
    }
}

How It Works

  1. Trailing spaces go. The three spaces after line are removed.
  2. Leading spaces stay. In " a " only the end is cleaned, so the two spaces after [ remain.
  3. The mirror of TrimStart. Same rules, opposite side.
  4. 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('.'));
    }
}

How It Works

  1. Repeats are removed together. All three ! marks are trailing, so they all go.
  2. Zeros after the last digit. 3.1400 loses its two trailing zeros. The zero-free part 3.14 is kept.
  3. A dot left alone. 5.000 loses all its zeros and becomes 5.; a second TrimEnd('.') removes the dot.
  4. 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);
    }
}

How It Works

  1. 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.
  2. Only line-break characters. Listing '\r' and '\n' removes the Windows line ending but would keep a trailing space or tab.
  3. Visible edges. The brackets show that nothing is left after data.
  4. 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.

Next: TrimStart()

Remove spaces (or chosen characters) from the start of a string only.

TrimStart() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful