Reputation: 115
Having a string HelloHello
how can I extract (i.e. omit) the first char, to have elloHello
?
I've thought of .at()
and string[n]
but they return the value and don't delete it from the string
Upvotes: 2
Views: 6412
Reputation: 3402
You should use substring. The first parameter indicates the start position. The second parameter string::npos
means you want the new string to contain all characters from the specified start position until the end of the string.
std::string shorterString = hellohello.substr(1, std::string::npos);
http://www.cplusplus.com/reference/string/string/substr/
Upvotes: 3
Reputation: 56539
Use erase
std::string str ("HelloHello");
str.erase (0,1); // Removes 1 characters starting at 0.
// ... or
str.erase(str.begin());
Upvotes: 3
Reputation: 150
Try using substr()
Reference: http://www.cplusplus.com/reference/string/string/substr/
Upvotes: 2
Reputation: 64308
#include <iostream>
#include <string>
int main(int,char**)
{
std::string x = "HelloHello";
x.erase(x.begin());
std::cout << x << "\n";
return 0;
}
prints
elloHello
Upvotes: 7