Sara Kholusi
Sara Kholusi

Reputation: 115

extract char from string

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

Answers (4)

Memento Mori
Memento Mori

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

masoud
masoud

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

Christopher J Smith
Christopher J Smith

Reputation: 150

Try using substr()

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

Upvotes: 2

Vaughn Cato
Vaughn Cato

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

Related Questions