How automatically remove all white space start or end in a string like:
"hello"
return "hello"
"hello "
return "hello"
" hello "
return "hello"
" hello world "
return "hello world"
regards
How automatically remove all white space start or end in a string like:
"hello"
return "hello"
"hello "
return "hello"
" hello "
return "hello"
" hello world "
return "hello world"
regards
use the String.Trim()
function.
string foo = " hello ";
string bar = foo.Trim();
Console.WriteLine(bar); // writes "hello"
string a = " Hello ";
string trimmed = a.Trim();
trimmed
is now "Hello"
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.
Try
string myString = " Hello World ";
myString = myString.TrimStart().TrimEnd();
String.Trim()
will trim all spaces from start and end of a string:
" A String " -> "A String"
String.TrimStart()
will trim all spaces from the start of a string:
" A String " -> "A String "
String.TrimEnd()
will trim all spaces from the end of a string:
" A String " -> " A String"