如果字符串包含多个 ,如何在每25个换行符( )上分割字符串

How to split a string on every 25th line break( ) if it contains multiple

本文关键字:字符串 换行符 25个 分割 包含多 如果      更新时间:2023-10-16

我已经读取了一个文件并将其内容存储到std::string变量BUF中,现在想将数据分成小块,每个块将包含25行。

我看到两个选项:

  • BUF构建std::istringstream(或使用读取文件的流),并使用std::getline逐行读取并附加到前几行,同时计数到25,等等…
  • 在计数到25的循环中使用std::string::find,然后使用std::string::substrstd::find并从迭代器范围中构造。

我成功了!

    if(N>25) \N no of lines
     {
         int i=0;
         std::istringstream ss(text);
         text="";
         string splited,part="";
         while(std::getline(ss, splited, 'n')) {
              part+=splited+"n";
              if(i==24)
              {
                i=0;
                part.erase(part.size() - 1);
                d.Insert(part,time);
                cout<<part;
                part="";
                continue;
              }
              else{
              ++i;
              continue;
              }
           }
           if(i!=0)
              {
                 d.Insert(part,time);
              }
     }
else{
        d.Insert(text,time);
     }