How can I remove last character from a C++ string?
I tried st = substr(st.length()-1);
But it didn't work.
Simple solution if you are using C++11. Probably O(1) time as well:
st.pop_back();
length()
.
For a non-mutating version:
st = myString.substr(0, myString.size()-1);
st = st.substr(0, st.size()-1)
. But it still doesn't look the right way, I think that the proper way is to use the function that is intended for this task, it's called erase() and the code is: st.erase(st.size()-1)
. This would be called a "mutating version".
Commented
Jan 19, 2013 at 14:46
pop_back
did not exist in C++03) and it is also an in-place modification (and the OP never clarified whether he wanted in-place or not)... as such, he has a correct answer, but not the only possible one.
Commented
May 21, 2013 at 6:24
That's all you need:
#include <string> //string::pop_back & string::empty
if (!st.empty())
st.pop_back();
if (str.size() > 0) str.resize(str.size() - 1);
An std::erase
alternative is good, but I like the - 1
(whether based on a size or end-iterator) - to me, it helps expresses the intent.
BTW - Is there really no std::string::pop_back
? - seems strange.
std::string::pop_back
in C++03; it's been added in C++0x, though.
Commented
Feb 22, 2010 at 14:20
resize
is a resizing function, and no more a memory function that anything else that might increase the memory needed. For example, if you resize
to a smaller size, it will not reduce the memory reserved. I think you're thinking of reserve
, which at least might reduce memory allocated if asked to - see resize here and reserve here.
buf.erase(buf.size() - 1);
This assumes you know that the string is not empty. If so, you'll get an out_of_range
exception.
size()
instead of end()
of another answer?
str[str.length()-1] = 0; str.erase(str.end()-1);
s[2]
is illegal.
int main () {
string str1="123";
string str2 = str1.substr (0,str1.length()-1);
cout<<str2; // output: 12
return 0;
}
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
str.erase(str.begin() + str.size() - 1)
str.erase(str.rbegin())
does not compile unfortunately, since reverse_iterator
cannot be converted to a normal_iterator.
C++11 is your friend in this case.
str.erase(str.end() - 1)
?
Commented
Dec 23, 2016 at 21:26
Don't worry about boundary check or empty string with ternary operator:
str.erase(str.end() - ((str.length() > 0) ? 1 : 0), str.end());
#include<iostream>
using namespace std;
int main(){
string s = "Hello";// Here string length is 5 initially
s[s.length()-1] = '\0'; // marking the last char to be null character
s = &s[0]; // using ampersand infront of the string with index will render a string from the index until null character discovered
cout<<"the new length of the string "<<s + " is " <<s.length();
return 0;
}
If the length is non zero, you can also
str[str.length() - 1] = '\0';
\0
does not change the length of the string. str.length()
will be inaccurate.
CString str=CString("Hello world"); str.Delete(str.GetLength()-1);