String.Trim Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Removes all leading and trailing white-space characters from the current string.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
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".
Note
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
Windows Phone OS
Supported in: 8.1, 8.0, 7.1, 7.0
Platforms
Windows Phone