.NET Framework Class Library
String.Trim Method (Char[])

Removes all occurrences of a set of characters specified in an array from the beginning and end of this instance.

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

Syntax

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
)
C++
public:
String^ Trim (
    ... array<wchar_t>^ trimChars
)
J#
public String Trim (
    char[] trimChars
)
JScript
public function Trim (
    ... trimChars : char[]
) : String

Parameters

trimChars

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

Return Value

The String that remains after all occurrences of the characters in trimChars are removed from the beginning and end of this instance. If trimChars is a null reference (Nothing in Visual Basic), white space characters are removed instead.
Remarks

For more information about what Unicode characters are categorized as white space characters, see the Remarks section of the Trim method overload.

Example

The following code example demonstrates the Trim(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
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);
    }
}
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 );
}
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);
    } //main
} //StringTrim2
Platforms

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
See Also

Tags :


Page view tracker