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.Trim Method (Char[])

Removes all leading and 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 Trim (
	params char[] trimChars
)
public String Trim (
	char[] trimChars
)
public function Trim (
	... 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 start and end of the current String object. If trimChars is a null reference (Nothing in Visual Basic), white-space characters are removed instead.

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".

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 the Trim(Char[]) method overload.

using System;

class stringTrim2 {
    public static void Main() {
        String str1 = "*;|@123***456@|;*";
	String delim = "*;|@";
	String str2 = str1.Trim(delim.ToCharArray());

	Console.WriteLine("Delimiters:      {0}", delim);
	Console.WriteLine("Original string: {0}", str1);
	Console.WriteLine("Trimmed string:  {0}", str2);
    }
}
// This code example displays the following:
//
// Delimiters:      *;|@
// Original string: *;|@123***456@|;*
// Trimmed string:  123***456

import System.*;

class StringTrim2
{
    public static void main(String[] args)
    {
        String str1 = "*;|@123***456@|;*";
        String delim = "*;|@";
        String str2 = str1.Trim(delim.ToCharArray());

        Console.WriteLine("Delimiters:      {0}", delim);
        Console.WriteLine("Original string: {0}", str1);
        Console.WriteLine("Trimmed string:  {0}", str2);
    } 
} 
// This code example displays the following:
//
// Delimiters:      *;|@
// Original string: *;|@123***456@|;*
// Trimmed string:  123***456

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

Community Additions

Show:
© 2014 Microsoft