Expand Minimize

String.TrimEnd Method

Updated: August 2008

Removes all trailing occurrences of a set of characters specified in an array from the current String object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public string TrimEnd(
	params char[] trimChars
)

Parameters

trimChars
Type: System.Char[]

An array of Unicode characters to remove or null.

Return Value

Type: System.String
The string that remains after all occurrences of the characters in the trimChars parameter are removed from the end of the current String object. If trimChars is null or an empty array, white-space characters are removed instead.

The TrimEnd method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when a character that is not in trimChars is encountered. For example, if the current string is "123abc456xyz789" and trimChars contains the digits from '1' through '9', the TrimEnd method returns "123abc456xyz".

NoteNote:

This method does not modify the value of the current instance. Instead, it returns a new string in which all trailing white space characters found in the current instance are removed.

For more information about which Unicode characters are categorized as white-space characters, see the Remarks section of the String.Trim() method overload.

The following example demonstrates how you can use the TrimEnd(Char[]) method overload to trim white space or punctuation marks from the end of a string.

using System;

public class TrimEnd
{
   public static void Main()
   {
      string sentence = "The dog had a bone, a ball, and other toys.";
      char[] charsToTrim = {',', '.', ' '};
      string[] words = sentence.Split();
      foreach (string word in words)
         Console.WriteLine(word.TrimEnd(charsToTrim));
   }
}
// The example displays the following output: 
//       The 
//       dog 
//       had 
//       a 
//       bone 
//       a 
//       ball 
//       and 
//       other 
//       toys

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

August 2008

Replaced the example.

Customer feedback.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft