String.Trim Method

Updated: April 2009

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

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

public string Trim()

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

NoteNote:

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.

If the current string equals Empty or all the characters in the current instance consist of white-space characters, the method returns Empty.

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 example uses the String.Trim() method to remove any extra white space from strings entered by the user before concatenating them.

using System;

public class Example
{
   public static void Main()
   {
      Console.Write("Enter your first name: ");
      string firstName = Console.ReadLine();

      Console.Write("Enter your middle name or initial: ");
      string middleName = Console.ReadLine();

      Console.Write("Enter your last name: ");
      string lastName = Console.ReadLine();

      Console.WriteLine();
      Console.WriteLine("You entered '{0}', '{1}', and '{2}'.", 
                        firstName, middleName, lastName);

      string name = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " + 
                    lastName.Trim()).Trim();
      Console.WriteLine("The result is " + name + ".");
   }
}
// The following is possible output from this example: 
//       Enter your first name:    John 
//       Enter your middle name or initial: 
//       Enter your last name:    Doe 
//        
//       You entered '   John  ', '', and '   Doe'. 
//       The result is John Doe

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, 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, Zune

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: 3.0, 2.0, 1.0

Date

History

Reason

April 2009

Expanded the Remarks section.

Customer feedback.

October 2008

Replaced the example.

Customer feedback.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft