How to remove all white space from the beginning or end of a string?

C#StringTrimRemoving Whitespace

C# Problem Overview


How can I remove all white space from the beginning and end of a string?

Like so:

"hello" returns "hello"
"hello " returns "hello"
" hello " returns "hello"
" hello world " returns "hello world"

C# Solutions


Solution 1 - C#

String.Trim() returns a string which equals the input string with all white-spaces trimmed from start and end:

"   A String   ".Trim() -> "A String"

String.TrimStart() returns a string with white-spaces trimmed from the start:

"   A String   ".TrimStart() -> "A String   "

String.TrimEnd() returns a string with white-spaces trimmed from the end:

"   A String   ".TrimEnd() -> "   A String"

None of the methods modify the original string object.

(In some implementations at least, if there are no white-spaces to be trimmed, you get back the same string object you started with:

csharp> string a = "a"; csharp> string trimmed = a.Trim(); csharp> (object) a == (object) trimmed; returns true

I don't know whether this is guaranteed by the language.)

Solution 2 - C#

take a look at Trim() which returns a new string with whitespace removed from the beginning and end of the string it is called on.

Solution 3 - C#

string a = "   Hello   ";
string trimmed = a.Trim();

trimmed is now "Hello"

Solution 4 - C#

use the String.Trim() function.

string foo = "   hello ";
string bar = foo.Trim();

Console.WriteLine(bar); // writes "hello"

Solution 5 - C#

Use String.Trim method.

Solution 6 - C#

String.Trim() removes all whitespace from the beginning and end of a string. To remove whitespace inside a string, or normalize whitespace, use a Regular Expression.

Solution 7 - C#

Trim() Removes all leading and trailing white-space characters from the current string. Trim(Char) Removes all leading and trailing instances of a character from the current string. Trim(Char[]) Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.

Look at the following example that I quoted from Microsoft's documentation page.

char[] charsToTrim = { '*', ' ', '\''};
string banner = "*** Much Ado About Nothing ***";
string result = banner.Trim(charsToTrim);
Console.WriteLine("Trimmmed\n   {0}\nto\n   '{1}'", banner, result);

// The example displays the following output:
//       Trimmmed
//          *** Much Ado About Nothing ***
//       to
//          'Much Ado About Nothing'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpedramView Question on Stackoverflow
Solution 1 - C#MauView Answer on Stackoverflow
Solution 2 - C#Russ CamView Answer on Stackoverflow
Solution 3 - C#adamseView Answer on Stackoverflow
Solution 4 - C#Adam RobinsonView Answer on Stackoverflow
Solution 5 - C#jwaliszkoView Answer on Stackoverflow
Solution 6 - C#tdammersView Answer on Stackoverflow
Solution 7 - C#Abdulhakim ZeinuView Answer on Stackoverflow