Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

String.Trim Method

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Function Trim As 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.

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.



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'


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

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.