C++按行拆分字符串

lmyy7pcs  于 2023-01-14  发布在  其他
关注(0)|答案(6)|浏览(196)

我需要一行一行地拆分字符串。我以前是这样做的:

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;
}

我输入了“we are one.\nyes we are.”并调用了doSegment方法。但是当我调试时,我发现语句参数是“we are one.\nyes we are.”,并且拆分失败。有人能告诉我为什么会发生这种情况以及我应该怎么做吗?有没有别的方法可以用C++来拆分字符串?谢谢!

2w2cym1i

2w2cym1i1#

我想使用std::getline或std::string::find来遍历字符串。下面的代码演示了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;
}
jaxagkaj

jaxagkaj2#

您可以在循环中调用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;
}

参见示例here

zsbz8rwp

zsbz8rwp3#

#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;
}
dbf7pr2w

dbf7pr2w4#

#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 line\n";
    sentence += "This is the second line\n";
    sentence += "This is the third line\n";

    vector<string> lines = splitter(R"(\n)", sentence);

    for (string line: lines){cout << line << endl;}

}

1.我们有一个多行字符串
1.我们把它们分成一个数组(向量)
1.我们在for循环中打印出这些元素

ycl3bljg

ycl3bljg5#

使用库range-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>>();
}
u7up0aaq

u7up0aaq6#

这种相当低效的方法只是循环遍历字符串,直到遇到一个\n换行符,然后创建一个子字符串并将其添加到向量中。

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;
}

相关问题