C# TrimEnd, Removing Trailing Chars

by Sam Allen - Updated January 15, 2010

You have a string with trailing punctuation, which you want to remove. Use TrimEnd to remove trailing punctuation. This is a common requirement when formatting text. Additionally, see examples of removing question marks, periods, and commas. Here we see how you can use the TrimEnd method in your C# programs.

TrimEnd removes single chars or several single chars.
You cannot specify an entire string.                 

Input:   Who's there?
         I am here...
Output:  Who's there
         I am here

Using TrimEnd

First, here we see an example program that loops through an array and trims its ending characters with TrimEnd. The array used is a string array. TrimEnd has similarities to the Trim and Split methods. However, it is used in different situations.

=== Program that uses TrimEnd (C#) ===

using System;

class Program
{
    static void Main()
    {
        // A.
        string[] items = new string[]
        {
            "Who's there?",
            "I am here..."
        };

        // B.
        foreach (string item in items)
        {
            // C.
            string trimmed = item.TrimEnd('?', '.', ',');
            Console.WriteLine(item);
            Console.WriteLine("    " + trimmed);
        }
        Console.ReadLine();
    }
}

=== Output of the program ===

Who's there?
    Who's there
I am here...
    I am here

Notes on the preceding example. In part A, string array is created. Your program will use a different set of strings. In part B, each string in the array is looped over with the foreach loop. In part C, TrimEnd is called, and it removes the question mark, period, and comma from the string.

Method signature

Here we note some aspects of the TrimEnd method in the BCL. TrimEnd receives an argument called a params argument. This allows you to use a special syntax to send characters to it. You don't need an array, although you can use an array.

=== Signature of TrimEnd method (C#) ===

string string.TrimEnd(param char[] trimChars)
Removes all trailing occurrences of a set of characters specified...

Using params

Here we look at some examples of different ways you can use arguments with TrimEnd. Sometimes you want to send TrimEnd an array you create. This allows you to easily change how you call TrimEnd. In other words, you can send the method an entire array, or just several parameters separated by commas.

=== Example of TrimEnd with many parameters (C#) ===

string t = s.TrimEnd('?', '.', ',');

=== Example of TrimEnd with array (C#) ===

char[] c = new char[]
{
    '?',
    '.',
    ','
};
string t = s.TrimEnd(c);

Using TrimStart

Here we note the very similar method called TrimStart in the C# language. TrimStart works the same way as TrimEnd. This article doesn't provide detailed examples because it is so similar. In my experience it is used less often, but is very useful.

(See Trim String Tips.)

Summary

Here we looked at different ways of using the TrimEnd method, including the params array and the array overload. Use TrimEnd and its friend TrimStart to remove characters from the end and start of strings. They remove all characters of the ones you specify until they hit a character they can't remove.

(Do not copy this page.)

Dot Net Perls | Search
Chars | ASCII Table | Char Array Use | Count Characters in String | ROT13 | Uppercase First Letter
C# | Obsolete Attribute | True and False | Gradient Tips | Catch Examples
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen