我应该如何在 c++ 中查找两个字符中的字符串
How should I go about finding a string within two chars in c++?
您好,我一直在尝试找到一种在两个字符中查找字符串的方法。我应该如何在 c++ 中执行此操作?
sdfgkjr$joeisawesome$sdfeids -> joeisawesome
编辑:另一个答案是寻找字符串中是否存在字符串。我正在寻找两个字符中的字符串,并在两个字符中输出刺痛。感谢您查看 PoX。
好的,所以当你说两个字符时,我假设你指的是分隔符。在这种情况下,您必须使用 String.find() 来查找分隔符的位置。 找到分隔符的位置后,可以使用 String.substr(index1,index2-index1) 返回子字符串。
例:
#include <iostream>
#include <string>
int main()
{
std::size_t index1,index2;
std::string myString = "sdfgkjr$joeisawesome$sdfeids";
std::string sub= "";
index1 = myString.find('$');
//string::npos is -1 if you are unaware
if(index1!=std::string::npos&& index1<myString.length()-1)
index2=myString.find('$',index1+1);
if(index2!=std::string::npos)
{
sub = myString.substr(index1+1,index2-index1);
}
std::cout<<sub; //outputs joeisawesome
}
相关文章:
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 在函数中连接两个字符,并在 C++ 中返回输出
- 舍入 QDecDouble 值,精度最多为两个字符
- 计算两个字符序列
- 在c++中连接两个字符
- 有没有办法我可以从 istringstream 中读取两个字符
- 如何查看两个字符变量是否相等
- 使用位于 C 字符串中的两个字符*获取子字符串
- 比较两个字符的问题
- 如何修复密码条目,它一次输入两个字符
- 字符串构造函数将两个字符* 放入另一个 std::string 在 C++14 中有效,但不适用于 C++17
- 将两个字符数组相互添加
- 在 c++ 中添加两个字符
- 如何删除QString的前两个字符
- 如何使用两个字符来控制 C++ 程序的流程
- 如何将两个字符或字符串数组附加到一个数组中
- 如何从两个字符之间的文件中提取整数
- 删除两个字符之间的文本
- std::setw() 将特殊字符视为两个字符
- 比较两个字符*是否相等