如何检查字符串的最后一个字符并查看其空格

17 c# asp.net string char lastindexof

如何检查字符串的最后一个字符并看到它的空格?如果它的空格被删除了吗?

Mic*_*gem 56

特定于一个空格字符:

if(MyString.EndsWith(" "))
    MyString = MyString.Substring(0, MyString.Length - 1);
Run Code Online (Sandbox Code Playgroud)

或任何空白

MyString = MyString.TrimEnd();
Run Code Online (Sandbox Code Playgroud)


Sam*_*ich 2

使用专门为其设计的功能Trim,,TrimStartTrimEnd

var trimmedString = "this is my string with space at the end ".TrimEnd();
Run Code Online (Sandbox Code Playgroud)

  • 需要明确的是,这将从字符串的前面和末尾删除所有空格,而不仅仅是从末尾删除一个空格。 (2认同)