Remove last character from C++ string

后端 未结 10 1259
慢半拍i
慢半拍i 2021-01-29 19:50

How can I remove last character from a C++ string?

I tried st = substr(st.length()-1); But it didn\'t work.

10条回答
  •  一整个雨季
    2021-01-29 20:12

    With C++11, you don't even need the length/size. As long as the string is not empty, you can do the following:

    if (!st.empty())
      st.erase(std::prev(st.end())); // Erase element referred to by iterator one
                                     // before the end
    

提交回复
热议问题