[英]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
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/
Try using substr() 尝试使用substr()
Reference: http://www.cplusplus.com/reference/string/string/substr/ 参考: http : //www.cplusplus.com/reference/string/string/substr/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.