String.Trim Method (array<Char>[]()[])

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

Syntax

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

Parameters

  • trimChars
    Type: array<System..::.Char>[]()[]
    An array of Unicode characters to remove or nullNothingnullptra 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 start and end of the current string. If trimChars is nullNothingnullptra null reference (Nothing in Visual Basic) or an empty array, white-space characters are removed instead.

Remarks

The Trim method removes from the current string all leading and trailing characters that are in the trimChars parameter. Each leading and trailing 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 Trim method returns "abc456xyz".

Note

This method does not modify the value of the current instance. Instead, it returns a new string in which all leading and trailing trimChars 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.

Examples

The following example uses the String..::.Trim(array<Char>[]()[]) method to remove any space, asterisk (*), and apostrophe (') characters from a string.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim charsToTrim() As Char = {"*"c, " "c, "'"c}
      Dim banner As String = "*** Much Ado About Nothing ***"
      Dim result As String = banner.Trim(charsToTrim)
      outputBlock.Text += String.Format("Trimmmed{0}   {1}{0}to{0}   '{2}'", _
                        vbCrLf, banner, result) & vbCrLf
   End Sub
End Module
' The example displays the following output:
'       Trimmmed
'          *** Much Ado About Nothing ***
'       to
'          'Much Ado About Nothing'
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      char[] charsToTrim = { '*', ' ', '\'' };
      string banner = "*** Much Ado About Nothing ***";
      string result = banner.Trim(charsToTrim);
      outputBlock.Text += String.Format("Trimmmed\n   {0}\nto\n   '{1}'", banner, result) + "\n";
   }
}
// The example displays the following output:
//       Trimmmed
//          *** Much Ado About Nothing ***
//       to
//          'Much Ado About Nothing'

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

String Class

Trim Overload

System Namespace

Char

TrimEnd

TrimStart