String.Trim Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Function Trim As String
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.

Remarks

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

Examples

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


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim temp As String() = MakeArray()


      outputBlock.Text &= String.Format("Concatenating the inital values in the array, we get the string:") & vbCrLf
      outputBlock.Text &= String.Format("'{0}'{1}", [String].Concat(temp), vbCrLf) & vbCrLf
      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
      outputBlock.Text &= String.Format("Concatenating the trimmed values in the array, we get the string:") & vbCrLf
      outputBlock.Text &= String.Format("'{0}'{1}", [String].Concat(temp), vbCrLf) & vbCrLf

      ' 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

      outputBlock.Text &= String.Format("Concatenating the start-trimmed values in the array, we get the string:") & vbCrLf
      outputBlock.Text &= String.Format("'{0}'{1}", [String].Concat(temp), vbCrLf) & vbCrLf

      ' 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

      outputBlock.Text &= String.Format("Concatenating the end-trimmed values in the array, we get the string:") & vbCrLf
      outputBlock.Text &= String.Format("'{0}'", [String].Concat(temp)) & vbCrLf
   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:
'
' 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'
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'

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.