Updated on Kisan Patel
If you have a string with a specific set of characters like delimiters, spaces, or some other characters at the beginning and/or end of the string and you want to trim the string then you need to use Trim
instance method:
string foo = " FIRST NAME "; Console.WriteLine(foo.Trim()); // it remove spaces and displays "FIRST NAME"
If you want to trim the specific characters at the beginning and/or end of the string:
string foo = "--FIRST NAME--"; Console.WriteLine(foo.Trim(new char[] {'-', ' '})); //it remove spaces and displays "FIRST NAME" Console.WriteLine(foo.TrimStart(new char[] {'-', ' '})); //remove from beginning and displays "FIRST NAME--" Console.WriteLine(foo.TrimEnd(new char[] {'-', ' '})); //remove at end and displays "--FIRST NAME"