Start learning free

LESSON 64 OF 674 · STRINGS I

String Strip / Trim in C#

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check