Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
String Class
String Methods
Trim Method
 Trim Method (Char[])
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
String..::.Trim Method (array<Char>[]()[])

Updated: November 2007

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)

Visual Basic (Declaration)
Public Function Trim ( _
    ParamArray trimChars As Char() _
) As String
Visual Basic (Usage)
Dim instance As String
Dim trimChars As Char()
Dim returnValue As String

returnValue = instance.Trim(trimChars)
C#
public string Trim(
    params char[] trimChars
)
Visual C++
public:
String^ Trim(
    ... array<wchar_t>^ trimChars
)
J#
public String Trim(
    char[] trimChars
)
JScript
public function Trim(
    ... trimChars : char[]
) : String

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 object. If trimChars is nullNothingnullptra 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".

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.

The following code example demonstrates the Trim(array<Char>[]()[]) method overload.

Visual Basic
Imports System
 _

Class stringTrim2

   Public Shared Sub Main()
      Dim str1 As [String] = "*;|@123***456@|;*"
      Dim delim As [String] = "*;|@"
      Dim str2 As [String] = str1.Trim(delim.ToCharArray())

      Console.WriteLine("Delimiters:      {0}", delim)
      Console.WriteLine("Original string: {0}", str1)
      Console.WriteLine("Trimmed string:  {0}", str2)
   End Sub 'Main
End Class 'stringTrim2

' This code example displays the following:
'
' Delimiters:      *;|@
' Original string: *;|@123***456@|;*
' Trimmed string:  123***456

C#
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

Visual C++
using namespace System;
int 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

J#
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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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

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: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker