
TrimEnd removes ending characters from a string. Let's suppose we have a string with trailing punctuation: we can use TrimEnd to remove these characters. This is a common requirement when formatting text. We can also remove question marks, periods and commas.
This C# article shows the use of the TrimEnd method on strings.
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
Who's there?
Who's there
I am here...
I am here
Notes. 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.

Here we note some aspects of the TrimEnd method in the .NET Framework. 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(params char[] trimChars) Removes all trailing occurrences of a set of characters specified...
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.
Params MethodExample 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);
It is possible to develop custom implementations of TrimEnd that are faster than the version included in the .NET Framework. To do this, you can use a for-loop from the last index of the string. Next, count the number of characters to remove; and finally return a substring using that value. Here's a fast method that removes trailing punctuation from a string.
Optimized trim implementation [C#]
static string TrimTrailingChars(string value)
{
int removeLength = 0;
for (int i = value.Length - 1; i >= 0; i--)
{
char let = value[i];
if (let == '?' ||
let == '!' ||
let == '.')
{
removeLength++;
}
else
{
break;
}
}
if (removeLength > 0)
{
return value.Substring(0, value.Length - removeLength);
}
return value;
}
Benchmark results
Input string:
const string input = "Dot Net Perls!?!...";
Notes:
TrimEnd version was called with a cached params array.
TrimTrailingChars: 34.87 ns
TrimEnd: 74.25 nsOptimization results: The optimized TrimTrailingChars method shown above performed more than twice as fast as the TrimEnd method. This could be a useful trick if you have a lot of trimming to do in an important program.

Here we note the 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 useful.
TrimStart Method
We looked at different ways of using the TrimEnd method, including the params array and the array overload. We 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.
String Type