移除前面有空格的前2个字符串
Remove first 2 strings that have spaces in front of it
我试图做我在标题中写的事情,但我遇到了问题。。当程序读取字符串以删除前面有空格的字符串时,将删除所有有空格的字符,直到他找到没有空格的字符串为止。。我试过这个:
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;
这样你就可以擦除前三个空格。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 两个字符串在 c++ 中不相等
- 构造<int>具有 2 个字符串文字的向量
- 移除前面有空格的前2个字符串
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- c++ 构造函数 将 1 个字符串参数转换为 3 个属性
- 比较两个字符串后卡在无限循环中
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 两个字符串之间的数学运算
- sscanf() 有两个字符串参数
- 我的字符串数组一次打印出前两个字符串
- 两个 c++ 字符串上的异或
- 函数返回两个字符串的第一个常用字符
- 目标是找到两个 c 字符串之间的公共前缀(必须使用特定的函数标头)
- 我需要在C++的两个字符串之间找到共同的前缀