Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Trim Method

July 28, 2014

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

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.

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.

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 Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      string[] temp = MakeArray();

      outputBlock.Text += String.Format("Concatenating the inital values in the array, we get the string:") + "\n";
      outputBlock.Text += String.Format("'{0}'{1}", String.Concat(temp), "\n") + "\n";

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

      outputBlock.Text += String.Format("Concatenating the trimmed values in the array, we get the string:") + "\n";
      outputBlock.Text += String.Format("'{0}'{1}", String.Concat(temp), "\n") + "\n";

      // 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);

      outputBlock.Text += String.Format("Concatenating the start-trimmed values in the array, we get the string:") + "\n";
      outputBlock.Text += String.Format("'{0}'{1}", String.Concat(temp), "\n") + "\n";

      // 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);

      outputBlock.Text += String.Format("Concatenating the end-trimmed values in the array, we get the string:") + "\n";
      outputBlock.Text += String.Format("'{0}'", String.Concat(temp)) + "\n";
   }

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

// This code example displays the following:
//
// 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 Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft