pedram pedram - 3 months ago 21
C# Question

How automatically remove all white spaces start or end in a string

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

Mau Mau
Answer

String.Trim() will trim all white-spaces from start and end of a string:

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

String.TrimStart() will trim all white-spaces from the start of a string:

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

String.TrimEnd() will trim all white-spaces from the end of a string:

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