移除前面有空格的前2个字符串

Remove first 2 strings that have spaces in front of it

本文关键字:2个 字符串 空格 前面      更新时间:2023-10-16

我试图做我在标题中写的事情,但我遇到了问题。。当程序读取字符串以删除前面有空格的字符串时,将删除所有有空格的字符,直到他找到没有空格的字符串为止。。我试过这个:

string s = "hello my name is SOMETHING";
size_t space_pos = s.rfind(" ") + 1;
cout << s.substr(space_pos) << "n";

输出:

SOMETHING

我需要的结果是:"name is SOMETHING"。我试着用replace,但前两个字符串("hello"answers"my"(的长度总是不同,从"hello"到"hello1"或"hello2818+"。

谢谢你,为我糟糕的英语感到抱歉。

rfind((查找给定字符串的最后一次出现。在该程序中,您正在创建一个子字符串,该子字符串的值仅在最后一次出现和字符串末尾之间。

string s = "hello my name is SOMETHING";
size_t space_pos = s.find(" ") + 1;
for(int i=0;i<2;i++){
s=s.substr(space_pos+1);
space_pos = s.find(" ");
}
std::cout<<s;

这样你就可以擦除前三个空格。