C++ split string by line

后端 未结 5 1448
我在风中等你
我在风中等你 2020-12-08 07:25

I need to split string by line. I used to do in the following way:

int doSegment(char *sentence, int segNum)
{
assert(pSegmenter != NULL);
Logger &log =          


        
相关标签:
5条回答
  • 2020-12-08 07:32
    #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. We have a string with multiple lines
    2. we split those into an array (vector)
    3. We print out those elements in a for loop
    0 讨论(0)
  • 2020-12-08 07:34

    I'd like to use std::getline or std::string::find to go through the string. below code demonstrates getline function

    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;
    }
    
    0 讨论(0)
  • 2020-12-08 07:35

    You can call std::string::find in a loop and the use 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;
    }
    

    See example here.

    0 讨论(0)
  • 2020-12-08 07:35
    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;
    }
    
    0 讨论(0)
  • 2020-12-08 07:45

    This fairly inefficient way just loops through the string until it encounters an \n newline escape character. It then creates a substring and adds it to a vector.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题