两个空白之间的输出
Output between two whitespaces
我有这个字符串:System->ONDRASHEK: Nick aaasssddd není v žádné místnosti
,我需要aaasssddd
作为字符串的输出。每次输出都不一样。所以它必须从两个空白处获得。我尝试过substr或split,但我对C++的了解很差。
我发现这个代码:
#include <string>
#include <iostream>
int main()
{
const std::string str = "System->ONDRASHEK: Nick aaasssddd není v žádné místnosti";
size_t pos = str.find(" ");
if (pos == std::string::npos)
return -1;
pos = str.find(" ", pos + 1);
if (pos == std::string::npos)
return -1;
std::cout << str.substr(pos, std::string::npos);
}
但不是,我需要的。
我假设您想要给定字符串中的第三个单词。
您已经找到了第二个空格,但您的输出是从第二个空间到字符串末尾的子字符串。
相反,您需要找到第三个空格,并输出这两个空格之间的子字符串。
所以这里是修改。
#include <string>
#include <iostream>
int main()
{
const std::string str = "System->ONDRASHEK: Nick aaasssddd není v žádné místnosti";
size_t pos = str.find(" ");
size_t start;
size_t end;
if (pos == std::string::npos)
return -1;
pos = str.find(" ", pos + 1);
if (pos == std::string::npos)
return -1;
start = pos + 1;
pos = str.find(" ", pos + 1);
if (pos == std::string::npos)
return -1;
end = pos;
std::cout << str.substr(start, end - start) << std::endl;
}
请详细说明您的问题?你需要2个空格之间的子串吗?若我是真的,先找到第一个空格,然后打印字符串,直到找到另一个空格。您可以为使用字符
相关文章:
- 为什么在其他输出线之间得到"1"输出?
- 如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值
- 如何使用 ALSA API 在应用程序中在单声道和立体声之间切换声音输出?
- 如何包含整数之间的空格作为输出
- Raku中C++和NativeCall之间的输出不同
- 无法在同一行上获取输出,在 C++ 中两个输出之间有空格
- 尝试查找数组中点之间的最小距离时的随机垃圾输出
- 为什么两个相同的代码给出不同的输出,而它们之间的唯一区别是不同的变量名称和写作样式
- 将线程之间的标准输入/输出重定向
- 提升正则表达式 cpp 用于查找 %% 之间的字符串,输出不包括 % 字符本身
- 这些代码之间的区别在哪里?输出不应该是一样的吗?
- (c++) 返回和函数输出之间的错误
- 在 c++ 中反转数组时,为什么在输入和输出之间得到一个"random"数字?
- GCC和MSVC之间的文件输出不一致
- 可视化GDB在MSVC2012和GCC 7.3.1之间打印不同的C++输出
- 为什么这个程序的输出在 C 和 C++ 之间不同?
- 两个空白之间的输出
- C++子级在复制构造函数和输出之间的某个时间点更改父成员
- 元素之间的输出逗号
- 在其他函数之间输出星期几的更好方法