How to check the last char of a string and see its a blank space

user339160 picture user339160 · Dec 9, 2011 · Viewed 48.1k times · Source

How to check the last char of a string and see its a blank space? If its a blank space remove it?

Answer

Michiel van Vaardegem picture Michiel van Vaardegem · Dec 9, 2011

Specific for one space character:

if(MyString.EndsWith(" "))
    MyString = MyString.Substring(0, MyString.Length - 1);

or for any whitespace

MyString = MyString.TrimEnd();