The Trim() Method is used to remove any unwanted character from the String.
public class MyApplication { public static void Main(string[] args) { string x = " Hello "; string y = x.Trim(); System.Console.WriteLine(y); } }
In the above code, we have declared a String ' Hello ' with leading and trailing spaces.
string x = " Hello ";
Then we have used the Trim() function to remove any unwanted spaces from beginning and end.
string y = x.Trim();
Unwanted spaces are stripped out of it.