Share via


Trimming and Removing Characters 

If you are parsing a sentence into individual words, you might end up with words that have blank spaces (also called white spaces) on either end of the word. In this situation, you can use one of the trim methods in the System.String class to remove any number of spaces or other characters from a specified position in the string. The following table describes the available trim methods.

Method name Use

String.Trim

Removes white spaces from the beginning and end of a string.

String.TrimEnd

Removes characters specified in an array of characters from the end of a string.

String.TrimStart

Removes characters specified in an array of characters from the beginning of a string.

String.Remove

Removes a specified number of characters from a specified index position in a string.

Trim

You can easily remove white spaces from both ends of a string using the String.Trim method, as shown in the following example.

Dim MyString As String = " Big   "
Console.WriteLine("Hello{0}World!", MyString)
Dim TrimString As String = MyString.Trim()
Console.WriteLine("Hello{0}World!", TrimString)
String MyString = " Big   ";
Console.WriteLine("Hello{0}World!", MyString );    
string TrimString = MyString.Trim();
Console.WriteLine("Hello{0}World!", TrimString );

This code displays the following lines to the console.

Hello Big   World!
HelloBigWorld! 

TrimEnd

The String.TrimEnd method removes characters from the end of a string, creating a new string object. An array of characters is passed to this method to specify the characters to be removed. The order of the elements in the character array does not affect the trim operation. The trim stops when a character not specified in the array is found.

The following example removes the last letters of a string using the TrimEnd method. In this example, the position of the 'r' character and the 'W' character are reversed to illustrate that the order of characters in the array does not matter. Notice that this code removes the last word of MyString plus part of the first.

Dim MyString As String = "Hello World!"
Dim MyChar As Char() =  {"r"c, "o"c, "W"c, "l"c, "d"c, "!"c, " "c }
Dim NewString As String = MyString.TrimEnd(MyChar)
Console.WriteLine(NewString)
string MyString = "Hello World!";
char[] MyChar = {'r','o','W','l','d','!',' '};
string NewString = MyString.TrimEnd(MyChar);
Console.WriteLine(NewString);

This code displays He to the console.

The following example removes the last word of a string using the TrimEnd method. In this code, a comma follows the word Hello and, because the comma is not specified in the array of characters to trim, the trim ends at the comma.

Dim MyString As String = "Hello, World!"
Dim MyChar As Char() =  {"r"c, "o"c, "W"c, "l"c, "d"c, "!"c, " "c }
Dim NewString As String = MyString.TrimEnd(MyChar)
Console.WriteLine(NewString)
string MyString = "Hello, World!";
char[] MyChar = {'r','o','W','l','d','!',' '};
string NewString = MyString.TrimEnd(MyChar);
Console.WriteLine(NewString);

This code displays Hello, to the console.

TrimStart

The String.TrimStart method is similar to the String.TrimEnd method except that it creates a new string by removing characters from the beginning of an existing string object. An array of characters is passed to the TrimStart method to specify the characters to be removed. As with the TrimEnd method, the order of the elements in the character array does not affect the trim operation. The trim stops when a character not specified in the array is found.

The following example removes the first word of a string. In this example, the position of the 'l' character and the 'H' character are reversed to illustrate that the order of characters in the array does not matter.

Dim MyString As String = "Hello World!"
Dim MyChar As Char() =  {"e"c, "H"c, "l"c, "o"c, " "c}
Dim NewString As String = MyString.TrimStart(MyChar)
Console.WriteLine(NewString)
string MyString = "Hello World!";
char[] MyChar = {'e', 'H','l','o',' ' };
string NewString = MyString.TrimStart(MyChar);
Console.WriteLine(NewString);

This code displays World! to the console.

Remove

The String.Remove method, beginning at a specified position in an existing string, removes a specified number of characters. This method assumes a zero-based index.

The following example removes ten characters from a string beginning at position five of a zero-based index of the string.

Dim MyString As String = "Hello Beautiful World!"
Console.WriteLine(MyString.Remove(5, 10))
string MyString = "Hello Beautiful World!";   
Console.WriteLine(MyString.Remove(5,10));

This code displays Hello World! to the console.

See Also

Other Resources

Basic String Operations