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

Updated: November 2007

Removes all leading and trailing white-space characters from the current String object.

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

Visual Basic (Declaration)
Public Function Trim As String
Visual Basic (Usage)
Dim instance As String
Dim returnValue As String

returnValue = instance.Trim()
C#
public string Trim()
Visual C++
public:
String^ Trim()
J#
public String Trim()
JScript
public function Trim() : String

Return Value

Type: System..::.String

The string that remains after all white-space characters are removed from the start and end of the current String object.

The Trim method removes from the current string all leading and trailing white-space characters. Each leading and trailing trim operation stops when a non-white-space character is encountered. For example, if the current string is "   abc   xyz   ", the Trim method returns "abc   xyz".

Note:

This method does not modify the value of the current instance. Instead, it returns a new string in which all leading and trailing white space characters found in the current instance are removed.

The following table lists the white-space characters removed by the Trim method. (Note that even though the static Char..::.IsWhiteSpace(Char) method returns true when passed a particular character, that character is not necessarily removed by the Trim method.) The first column lists the Unicode name for the character, and the second column lists the hexadecimal notation for the Unicode code point that identifies the character.

Unicode name

Unicode code point

CHARACTER TABULATION

U+0009

LINE FEED

U+000A

LINE TABULATION

U+000B

FORM FEED

U+000C

CARRIAGE RETURN

U+000D

SPACE

U+0020

NEXT LINE

U+0085

NO-BREAK SPACE

U+00A0

OGHAM SPACE MARK

U+1680

EN QUAD

U+2000

EM QUAD

U+2001

EN SPACE

U+2002

EM SPACE

U+2003

THREE-PER-EM SPACE

U+2004

FOUR-PER-EM SPACE

U+2005

SIX-PER-EM SPACE

U+2006

FIGURE SPACE

U+2007

PUNCTUATION SPACE

U+2008

THIN SPACE

U+2009

HAIR SPACE

U+200A

ZERO WIDTH SPACE

U+200B

LINE SEPARATOR

U+2028

PARAGRAPH SEPARATOR

U+2029

IDEOGRAPHIC SPACE

U+3000

ZERO WIDTH NO-BREAK SPACE

U+FEFF

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

Visual Basic
Imports System

Public Class TrimTest

    Public Shared Sub Main()
        Dim temp As String() = MakeArray()


        Console.WriteLine("Concatenating the inital values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)
        Dim i As Integer

        ' trim whitespace from both ends of the elements
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).Trim()

        Next i
        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)

        ' reset the array
        temp = MakeArray()

        ' trim the start of the elements. Passing null trims whitespace only
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).TrimStart(" "c)
        Next i

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'{1}", [String].Concat(temp), Environment.NewLine)

        ' reset the array
        temp = MakeArray()

        ' trim the end of the elements. Passing null trims whitespace only
        For i = 0 To temp.Length - 1
            temp(i) = temp(i).TrimEnd(" "c)
        Next i

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:")
        Console.WriteLine("'{0}'", [String].Concat(temp))
    End Sub 'Main

    Private Shared Function MakeArray() As String()
        Dim arr As String() = {"  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "}
        Return arr
    End Function 'MakeArray
End Class 'TrimTest

' This code example displays the following to the console:
'
' Concatenating the inital values in the array, we get the string:
' '  please      tell      me      about      yourself    '
'
' Concatenating the trimmed values in the array, we get the string:
' 'pleasetellmeaboutyourself'
'
' Concatenating the start-trimmed values in the array, we get the string:
' 'please    tell    me    about    yourself    '
'
' Concatenating the end-trimmed values in the array, we get the string:
' '  please  tell  me  about  yourself'

C#
using System;

public class TrimTest {
    public static void Main() {

        string [] temp = MakeArray();

        Console.WriteLine("Concatenating the inital values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // trim whitespace from both ends of the elements
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].Trim();

        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the start of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimStart(null);

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the end of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimEnd(null);

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'", String.Concat(temp));
    }

    private static string [] MakeArray() {
        string [] arr = {"  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "};
        return arr;
    }
}

// This code example displays the following to the console:
//
// Concatenating the inital values in the array, we get the string:
// '  please      tell      me      about      yourself    '
//
// Concatenating the trimmed values in the array, we get the string:
// 'pleasetellmeaboutyourself'
//
// Concatenating the start-trimmed values in the array, we get the string:
// 'please    tell    me    about    yourself    '
//
// Concatenating the end-trimmed values in the array, we get the string:
// '  please  tell  me  about  yourself'

Visual C++
using namespace System;

array<String^>^ MakeArray()
{
   array<String^>^arr = {"  please    ","  tell    ","  me    ","  about    ","  yourself    "};
   return arr;
}

int main()
{
   array<String^>^temp = MakeArray();
   Console::WriteLine( "Concatenating the inital values in the array, we get the string:" );
   Console::WriteLine( "'{0}'\n", String::Concat( temp ) );

   // trim whitespace from both ends of the elements
   for ( int i = 0; i < temp->Length; i++ )
      temp[ i ] = temp[ i ]->Trim();
   Console::WriteLine( "Concatenating the trimmed values in the array, we get the string:" );
   Console::WriteLine( "'{0}'", String::Concat( temp ) );

   // reset the array
   temp = MakeArray();

   // trim the start of the elements-> Passing 0 trims whitespace only
   for ( int i = 0; i < temp->Length; i++ )
      temp[ i ] = temp[ i ]->TrimStart( 0 );
   Console::WriteLine( "Concatenating the start-trimmed values in the array, we get the string:" );
   Console::WriteLine( "'{0}'\n", String::Concat( temp ) );

   // reset the array
   temp = MakeArray();

   // trim the end of the elements-> Passing 0 trims whitespace only
   for ( int i = 0; i < temp->Length; i++ )
      temp[ i ] = temp[ i ]->TrimEnd( 0 );
   Console::WriteLine( "Concatenating the end-trimmed values in the array, we get the string:" );
   Console::WriteLine( "'{0}'", String::Concat( temp ) );
}
// This code example displays the following to the console:
//
// Concatenating the inital values in the array, we get the string:
// '  please      tell      me      about      yourself    '
//
// Concatenating the trimmed values in the array, we get the string:
// 'pleasetellmeaboutyourself'
//
// Concatenating the start-trimmed values in the array, we get the string:
// 'please    tell    me    about    yourself    '
//
// Concatenating the end-trimmed values in the array, we get the string:
// '  please  tell  me  about  yourself'

J#
import System.*;

public class TrimTest
{
    public static void main(String[] args)
    {
        String temp[] = MakeArray();

        Console.WriteLine("Concatenating the inital values in the array," 
            + " we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp),
            Environment.get_NewLine());
        // trim whitespace from both ends of the elements
        for (int i = 0; i < temp.get_Length(); i++) {
            temp.set_Item(i, temp[i].Trim());
        }
        Console.WriteLine("Concatenating the trimmed values in the array," 
            + " we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), 
            Environment.get_NewLine());
        // reset the array
        temp = MakeArray();
        // trim the start of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.get_Length(); i++) {
            temp.set_Item(i, temp[i].TrimStart(null));
        }
        Console.WriteLine("Concatenating the start-trimmed values in the " 
            + "array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), 
            Environment.get_NewLine());
        // reset the array
        temp = MakeArray();
        // trim the end of the elements. Passing null trims whitespace only
        for (int i = 0; i < temp.get_Length(); i++) {
            temp.set_Item(i, temp[i].TrimEnd(null));
        }
        Console.WriteLine("Concatenating the end-trimmed values in the array," 
            + " we get the string:");
        Console.WriteLine("'{0}'", String.Concat(temp));
    } //main

    private static String[] MakeArray()
    {
        String arr[] =  { "  please    ", "  tell    ", "  me    ", 
            "  about    ", "  yourself    " };
        return arr;
    } //MakeArray
} //TrimTest

// This code example displays the following to the console:
//
// Concatenating the inital values in the array, we get the string:
// '  please      tell      me      about      yourself    '
//
// Concatenating the trimmed values in the array, we get the string:
// 'pleasetellmeaboutyourself'
//
// Concatenating the start-trimmed values in the array, we get the string:
// 'please    tell    me    about    yourself    '
//
// Concatenating the end-trimmed values in the array, we get the string:
// '  please  tell  me  about  yourself'

JScript
import System;

public class TrimTest {
    public static function Main() : void {

        var temp : String [] = MakeArray();

        Console.WriteLine("Concatenating the inital values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // trim whitespace from both ends of the elements
        for (var i : int = 0; i < temp.Length; i++)
            temp[i] = temp[i].Trim();

        Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

    var c : char[] = undefined;

        // trim the start of the elements. Passing null trims whitespace only
        for (i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimStart(c);

        Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);

        // reset the array
        temp = MakeArray();

        // trim the end of the elements. Passing null trims whitespace only
        for (i = 0; i < temp.Length; i++)
            temp[i] = temp[i].TrimEnd(c);

        Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
        Console.WriteLine("'{0}'", String.Concat(temp));
    }

    private static function MakeArray() : String [] {
        var arr : String [] = ["  please    ", "  tell    ", "  me    ", "  about    ", "  yourself    "];
        return arr;
    }
}
TrimTest.Main();

// This code example displays the following to the console:
//
// Concatenating the inital values in the array, we get the string:
// '  please      tell      me      about      yourself    '
//
// Concatenating the trimmed values in the array, we get the string:
// 'pleasetellmeaboutyourself'
//
// Concatenating the start-trimmed values in the array, we get the string:
// 'please    tell    me    about    yourself    '
//
// Concatenating the end-trimmed values in the array, we get the string:
// '  please  tell  me  about  yourself'

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
I don 't understand this:      BigDal   |   Edit   |  

"Country Canada".TrimStart("Country ".ToCharArray())

"anada"

"Country Lanada".TrimStart("Country ".ToCharArray())

"Lanada"

Am I missing something?

Tags What's this?: Add a tag
Flag as ContentBug
Not a bug      Bill.Y   |   Edit   |  

It does not Trim by string, instead it trims by character, so all C,o,u,n,t,r,y CHARs are trimmed from the beginning, until it hits first non-match.

In your first case, it hits 'a' and stopped there, while in the second case it stopped at L.

string.TrimStart Method:

http://msdn.microsoft.com/en-us/library/system.string.trimstart.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker