Trimming of string in C#


In C#, we use Trim function to remove spaces or new lines from the beginning or ending of string.

There are three Trim methods in .net String Class-
1. Trim
2. TrimStart
3. TrimEnd


// Input String
String st = “      Hello World       ”;
// Call Trim instance method.
String st_Trim = st.Trim();
String st_TrimStart = st.TrimStart();
String st_TrimEnd = st.TrimEnd();


st_Trim = “Hello World”
st_TrimStart = “Hello World ”
st_TrimEnd = “ Hello World”

  • Mickey Byrdsong

    Thanks for sharing. really helps me a lot.