31
Use std::string::substr:
try {
str = str.substr(10);
} catch (std::out_of_range&) {
//oops str is too short!!!
}
I suspect that there is more code here that you are not showing, and the problem is likely there.
This code works just fine:
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
string imgURL = "<img src=\"http://imgs.xkcd.com/comics/sky.png";
string str = imgURL;
int urlLength = imgURL.length();
urlLength = urlLength-10;
str.erase (str.begin(), str.end()-urlLength);
imgURL = str;
cout << imgURL << endl;
return 0;
}
With that said, there are shorter ways to do this, as others have mentioned.