In C#, you can remove leading and trailing whitespace from a string using Trim():
s.Trim() // removes leading and trailing whitespace
s.TrimStart() // removes only leading whitespace
s.TrimEnd() // removes only trailing whitespace
Here are some examples:
string s = " hello ";
Console.WriteLine(s.Trim()); // "hello"
Console.WriteLine(s.TrimStart()); // "hello "
Console.WriteLine(s.TrimEnd()); // " hello"
These methods return a new string — the original is not modified.
Assignment
Follow the Coding Tutorial and strip whitespace from s!
Hint
Use s.Trim() and assign the result to result.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
