C 按行分开字符串
C++ split string by line
本文关键字:字符串 更新时间:2023-10-16
我需要按行拆分字符串。我曾经以下面的方式做:
int doSegment(char *sentence, int segNum)
{
assert(pSegmenter != NULL);
Logger &log = Logger::getLogger();
char delims[] = "n";
char *line = NULL;
if (sentence != NULL)
{
line = strtok(sentence, delims);
while(line != NULL)
{
cout << line << endl;
line = strtok(NULL, delims);
}
}
else
{
log.error("....");
}
return 0;
}
i输入"我们是一个。 nyes我们是。"并调用剂量段方法。但是,当我调试时,我发现句子参数是"我们是一个。\ nyes我们是",而拆分失败了。有人可以告诉我为什么会发生这种情况以及我该怎么办。无论如何,还有其他可以用来在C 中拆分字符串的。谢谢!
我想使用std :: getline或std :: string :: find find thing the String。下面的代码演示了getline函数
int doSegment(char *sentence)
{
std::stringstream ss(sentence);
std::string to;
if (sentence != NULL)
{
while(std::getline(ss,to,'n')){
cout << to <<endl;
}
}
return 0;
}
您可以在循环中调用std::string::find
,并使用std::string::substr
。
std::vector<std::string> split_string(const std::string& str,
const std::string& delimiter)
{
std::vector<std::string> strings;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find(delimiter, prev)) != std::string::npos)
{
strings.push_back(str.substr(prev, pos - prev));
prev = pos + delimiter.size();
}
// To get the last substring (or only, if delimiter is not found)
strings.push_back(str.substr(prev));
return strings;
}
请参见此处的示例。
#include <sstream>
#include <string>
#include <vector>
std::vector<std::string> split_string_by_newline(const std::string& str)
{
auto result = std::vector<std::string>{};
auto ss = std::stringstream{str};
for (std::string line; std::getline(ss, line, 'n');)
result.push_back(line);
return result;
}
#include <iostream>
#include <string>
#include <regex>
#include <algorithm>
#include <iterator>
using namespace std;
vector<string> splitter(string in_pattern, string& content){
vector<string> split_content;
regex pattern(in_pattern);
copy( sregex_token_iterator(content.begin(), content.end(), pattern, -1),
sregex_token_iterator(),back_inserter(split_content));
return split_content;
}
int main()
{
string sentence = "This is the first linen";
sentence += "This is the second linen";
sentence += "This is the third linen";
vector<string> lines = splitter(R"(n)", sentence);
for (string line: lines){cout << line << endl;}
}
- 我们有一个带有多行的字符串
- 我们将它们分为数组(vector)
- 我们将这些元素在for循环中打印出来
使用库范围-v3:
#include <range/v3/all.hpp>
#include <string>
#include <string_view>
#include <vector>
std::vector<std::string> split_string_by_newline(const std::string_view str) {
return str | ranges::views::split('n')
| ranges::to<std::vector<std::string>>();
}
使用C 23范围:
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
std::vector<std::string> split_string_by_newline(const std::string_view str) {
return str | std::ranges::views::split('n')
| std::ranges::to<std::vector<std::string>>();
}
这种效率相当低的方式只需循环循环,直到遇到 n newline逃生字符。然后,它创建一个子字符串并将其添加到向量。
std::vector<std::string> Loader::StringToLines(std::string string)
{
std::vector<std::string> result;
std::string temp;
int markbegin = 0;
int markend = 0;
for (int i = 0; i < string.length(); ++i) {
if (string[i] == 'n') {
markend = i;
result.push_back(string.substr(markbegin, markend - markbegin));
markbegin = (i + 1);
}
}
return result;
}