The Trim()
method in C# is used to remove any leading and trailing whitespace characters in a string. This means that it removes any whitespace that begins or ends a string.
str.Trim()
None.
It returns a string that contains no whitespace.
In the code example below, we will create some strings with whitespaces and remove those that start or ends them.
// creat classclass Trimmer{// main methodstatic void Main(){// create stringsstring str1 = " Welcome to Edpresso! ";string str2 = "Thedore Onyejiaku ";string str3 = " a b c d ef ";// trim stringsstring a = str1.Trim();string b = str2.Trim();string c = str3.Trim();// print returned valuesSystem.Console.WriteLine(a);System.Console.WriteLine(b);System.Console.WriteLine(c);}}