Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.TrimEnd Method

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
)
public String TrimEnd (
	char[] trimChars
)
public function TrimEnd (
	... trimChars : char[]
) : String
Not applicable.

Parameters

trimChars

An array of Unicode characters to remove or a null reference (Nothing in Visual Basic).

Return Value

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 a null reference (Nothing in Visual Basic), 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".

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 code example demonstrates how you can use the TrimEnd method overload to trim white space or other characters from the end of a string.

using System;


namespace String_Example
{
    class Application
    {
        public static void Main()
        {
            // Create a string that will be trimmed.
            string path = "c:/temp//";

            // Create an array of characters 
            // that represent characters to trim.
            char[] charsToTrim = {'/'};

            // Thim the string.
            string trimmedPath = path.TrimEnd(charsToTrim);

            Console.WriteLine("The trimmed value is: {0}.", trimmedPath);

            // Create a string that will be trimmed.
            string pathWhitespace = "c:/temp/  ";

            // Trim whitespaces by passing null.
            string trimmedWhiteSpace = pathWhitespace.TrimEnd(null);

            Console.WriteLine("The trimmed value is: {0}.", trimmedWhiteSpace);
        }

    }
}
// This code example displays the following
// to the console:
//
// The trimmed value is: c:/temp.
// The trimmed value is: c:/temp/.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft