gpt4 book ai didi

c++ - 如何在 cpp 中使用 CRLF 分隔符拆分字符串?

转载 作者:行者123 更新时间:2023-11-30 05:44:48 24 4
gpt4 key购买 nike

我有一些字符串:

testing testing

test2test2

这些行由 CRLF 分隔。我看到有:0d0a0d0a deviding them。我如何使用这些信息拆分它?

我想使用 str.find(CRLF-DELIMITER) 但不知道如何使用

编辑:我已经使用了 str.find("textDelimiter"),但现在我需要它来查找 hexa 而不是搜索字符串 "0d0a0d0a"

最佳答案

使用 boost::split 来做到这一点。另请查看 Boost.Tokenizer

这是使用正则表达式的另一种方法:

using std::endl;
using std::cout;
using std::string;
using std::vector;
using boost::algorithm::split_regex;

int main()
{
vector<string> res;
string input = "test1\r\ntest2\r\ntest3";
split_regex(res, input, boost::regex("(\r\n)+"));
for (auto& tok : res)
{
std::cout << "Token: " << tok << std::endl;
}
return 0;
}

下面是不使用 Boost 的方法:

 #include <string>
#include <sstream>
#include <istream>
#include <vector>
#include <iostream>

int main()
{
std::string strlist("line1\r\nLine2\r\nLine3\r\n");
std::istringstream MyStream(strlist);
std::vector<std::string> v;
std::string s;
while (std::getline(MyStream, s))
{
v.push_back(s);
std::cout << s << std::endl;
}
return 0;
}

关于c++ - 如何在 cpp 中使用 CRLF 分隔符拆分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29622061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com