Cosmic Q&A
How can I trim spaces from a string in C#?
Answer
We can trim spaces from a string in C# using Trim, TrimStart and TrimEnd methods.
string word = " Hello World ";
// Trim all spaces from front and back.
Console.WriteLine(word.Trim());
// Trim spaces from beginning of the String
Console.WriteLine(word.TrimStart());
// Trim spaces from end of the String
Console.WriteLine(word.TrimEnd());
Share this page:
Report Error in Content