简体   繁体   English

从字符串中提取char

[英]extract char from string

Having a string HelloHello how can I extract (ie omit) the first char, to have elloHello ? 有一个字符串HelloHello如何提取(即省略)第一个字符,以获得elloHello

I've thought of .at() and string[n] but they return the value and don't delete it from the string 我想过.at()string[n]但它们返回值并且不从字符串中删除它

#include <iostream>
#include <string>

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

prints 版画

elloHello

Use erase 使用erase

std::string str ("HelloHello");

str.erase (0,1);  // Removes 1 characters starting at 0.

// ... or

str.erase(str.begin());

You should use substring. 你应该使用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. 第二个参数string::npos表示您希望新字符串包含从指定起始位置到字符串结尾的所有字符。

std::string shorterString = hellohello.substr(1,  std::string::npos); 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM