41

我需要逐行拆分字符串。我以前是按以下方式做的:

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

我输入“我们是一个。\是的,我们是。” 并调用 doSegment 方法。但是当我调试时,我发现句子参数是“we are one.\\nyes we are”,并且拆分失败。有人可以告诉我为什么会这样,我该怎么办。还有其他我可以用来在 C++ 中拆分字符串的方法吗?谢谢 !

4

5 回答 5

75

我想使用 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;
}
于 2012-11-01T07:14:33.253 回答
17

您可以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 + 1;
    }

    // To get the last substring (or only, if delimiter is not found)
    strings.push_back(str.substr(prev));

    return strings;
}

请参见此处的示例。

于 2012-11-01T07:08:17.420 回答
13
#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;
}
于 2019-12-10T12:29:57.173 回答
2
#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. 我们有一个多行的字符串
  2. 我们将它们分成一个数组(向量)
  3. 我们在 for 循环中打印出这些元素
于 2018-02-17T02:41:59.287 回答
-1

这种相当低效的方法只是循环遍历字符串,直到遇到 \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;
}
于 2019-05-28T13:22:55.153 回答