257

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"

archer
  • 262
  • 2
  • 15
pedram
  • 3,647
  • 6
  • 24
  • 28

7 Answers7

541

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.)

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
Mau
  • 14,234
  • 2
  • 31
  • 52
  • 1
    ⁺¹ for the MS definition of whitespace. I met a weird behavior that `.TrimEnd()` doesn't work *(for non-breaking space character)*, but in the end is just that the character not listed in documentation. – Hi-Angel Oct 21 '15 at 14:18
  • 3
    There are numerous ways to trim strings, and [quite a few are bench-marked.](http://cc.davelozinski.com/c-sharp/fastest-way-to-trim-strings) Still, I like .Trim() as being the quickest to write and easiest to read. –  Aug 15 '16 at 09:43
  • 1
    Maybe it is usefull to know this: If you have multiply lines like in a TextArea. And you press the enter key, you get something like: `" A String \r\n "` `.Trim()` does recognize this as a space too. – Nash Carp Nov 10 '17 at 10:20
  • @NashCarp: That´s because \r and \n are also [whitespace characters](https://msdn.microsoft.com/en-us/library/t809ektx%28v=vs.110%29.aspx) – huha Mar 08 '19 at 12:41
24

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.

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
22
string a = "   Hello   ";
string trimmed = a.Trim();

trimmed is now "Hello"

ase
  • 13,231
  • 4
  • 34
  • 46
17

use the String.Trim() function.

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

Console.WriteLine(bar); // writes "hello"
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
13

Use String.Trim method.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
jwaliszko
  • 16,942
  • 22
  • 92
  • 158
10

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.

tdammers
  • 20,353
  • 1
  • 39
  • 56
4

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'
Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37