Export (0) Print
Expand All

String.Trim Method ()

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

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

public string Trim ()
public String Trim ()
public function Trim () : String
Not applicable.

Return Value

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

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

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'

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'

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

ADD
Show:
© 2014 Microsoft