如何在多个不同字符之间获取多个字符串
How can i get multiple string in between multiple different characters?
我已经搜索了你的几个主题,但我似乎无法弄清楚如何在多个不同的字符之间切换。
例
string str = "AG/B/C,BC/D,AD,AE/R/B/A,AB";
我想在两个/或两个之间提取,只有
最终结果应该是:
B,AD,R,B,AB
string temp;
for(int i=0; i < str.size(); i++)
{
temp += str[i];
if((str[i] == '/') || str[i] == ',')
{
//do something
}
}
string temp = "";
char flag = ' ';
int cursor = 0;
for(int i = 0; i < str.size(); i++)
{
if(str[i] == '/' || str[i] == ',')
{
if(flag == str[i] && cursor < i-1)
temp+=str.substr(cursor+1,i-cursor-1)+",";
flag = str[i];
cursor = i;
}
}
这是一个复杂的用例,因为您希望分隔符成对执行。当然可以使用正则表达式来实现这一点,但恕我直言,读取写入都不会比直接处理更简单。只需存储最后一个分隔符。
string str = "AG/B/C,BC/D,AD,AE/R/B/A";
char last = ' '; // initialize last separator to an impossible value
string temp;
for(size_t i=0; i < str.size(); i++)
{
if((str[i] == '/') || str[i] == ',') // found a separator
{
if (last == str[i]) { // is it the second on a pair
std::cout << temp << std::endl; // or store temp in a vector for later use...
}
last = str[i]; // store last separator for future use
temp.clear(); // reset the temp string
}
else temp.push_back(str[i]); // non separator chars are added to temp string
}
相关文章:
- 如何以毫秒为单位获取开始时间和 now() 之间的毫秒差异(以 C++为单位?
- 获取 2 个数字之间的差异百分比
- 比较它们之间的元素并获取公共值的键
- 如何获取两个时间点之间的时间
- 如何仅在 2 个节点之间获取最短路径,给定邻接列表有向图?
- C++获取两个分隔符之间的字符串并替换它
- 高CPU使用率,在API桌面复制中获取帧之间具有不同的超时间隔
- 正则表达式 获取两个换行符之间的文本
- 获取两个数组之间的差异
- 多个线程之间的获取-释放内存顺序
- 在文本中的两个非唯一字符串之间获取子弦
- 如何在多个不同字符之间获取多个字符串
- 在两天之间获取交易天数
- 从两个单词之间获取字符串
- C++如何在两个单词之间获取字符串/字符
- 在分隔符之间获取字符串
- 在两个迭代器之间获取“std::string”的子字符串
- Boost regex token迭代器:在括号之间获取输入
- 在使用相同C++代码的跨平台窗体之间获取运行时错误
- 如何在两个提升时间之间获取每个单独的日期