2

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

1
  • 1
    What type is your string? char* or std::string? Commented Mar 26, 2013 at 14:29

4 Answers 4

7
#include <iostream> #include <string> int main(int,char**) { std::string x = "HelloHello"; x.erase(x.begin()); std::cout << x << "\n"; return 0; } 

prints

elloHello 
Sign up to request clarification or add additional context in comments.

2 Comments

what is this operator called :: ?
@SaraKholusi: That is the scope resolution operator.
3

Use erase

std::string str ("HelloHello"); str.erase (0,1); // Removes 1 characters starting at 0. // ... or str.erase(str.begin()); 

Comments

3

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/

Comments

2

Try using substr()

Reference: http://www.cplusplus.com/reference/string/string/substr/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.