Partager via


String.TrimEnd Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Function TrimEnd ( _
    ParamArray trimChars As Char() _
) As String
public string TrimEnd(
    params char[] trimChars
)

Parameters

  • trimChars
    Type: array<System.Char[]
    An array of Unicode characters to remove or nulla null reference (Nothing in Visual Basic).

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. If trimChars is nulla null reference (Nothing in Visual Basic) or an empty array, white-space characters are removed instead.

Remarks

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 characters found in trimChars are removed from the current string.

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

Examples

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

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim sentence As String = "The dog had a bone, a ball, and other toys."
      Dim charsToTrim() As Char = {","c, "."c, " "c}
      Dim words() As String = sentence.Split()
      For Each word As String In words
         outputBlock.Text &= word.TrimEnd(charsToTrim) & vbCrLf
      Next
   End Sub
End Module
' The example displays the following output:
'       The
'       dog
'       had
'       a
'       bone
'       a
'       ball
'       and
'       other
'       toys
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string sentence = "The dog had a bone, a ball, and other toys.";
      char[] charsToTrim = {',', '.', ' '};
      string[] words = sentence.Split();
      foreach (string word in words)
         outputBlock.Text += word.TrimEnd(charsToTrim) + "\n";
   }
}
// The example displays the following output:
//       The
//       dog
//       had
//       a
//       bone
//       a
//       ball
//       and
//       other
//       toys

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.