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"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
String.Trim()returns a string which equals the input string with all white-spaces trimmed from start and end:String.TrimStart()returns a string with white-spaces trimmed from the start:String.TrimEnd()returns a string with white-spaces trimmed from the end: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.)