Trim
ExamplesΒΆ
The general syntax for this method is:
stringName.Trim();
This method returns a new string with any leading or trailing whitespace removed. Whitespace characters are those that do not display anything on the screen, such as spaces and tabs.
Example
1 2 3 | Console.WriteLine("Saint Louis ".Trim());
Console.WriteLine(" Saint Louis".Trim());
Console.WriteLine(" Saint Louis ".Trim());
|
Output
Saint Louis
Saint Louis
Saint Louis
Example
When typing an email address into a web site, a user may inadvertently type a space before and/or after the email address. We can clean up such input using the trim
method.
This example cleans up user input with trim
.
string input = " [email protected] ";
string email = input.Trim();
Console.WriteLine(email);
Output