如果字符串包含多个 ,如何在每25个换行符( )上分割字符串
How to split a string on every 25th line break( ) if it contains multiple
我已经读取了一个文件并将其内容存储到std::string
变量BUF
中,现在想将数据分成小块,每个块将包含25行。
我看到两个选项:
- 从
BUF
构建std::istringstream
(或使用读取文件的流),并使用std::getline
逐行读取并附加到前几行,同时计数到25,等等… - 在计数到25的循环中使用
std::string::find
,然后使用std::string::substr
或std::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);
}
相关文章:
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- 在字符串中输入换行符
- 使用 getline 读取字符串,但每行中都有换行符
- 删除字符串数组中的换行符
- C++ cout 在擦除换行符后打印字符串不正确
- 如何在C++中检测字符串输入中的换行符
- 如何使用ifstream c ++从带有换行符的文件读取字符串
- 在命令提示符下看到换行符,但在文件输出中可以看到 相同的字符串
- 如何仅当字符串流包含任何换行符时才从字符串流中读取行
- 为什么此代码在遇到换行符之前无法正确读取字符串?
- 简单的字符串输出不符合预期(出现换行符)
- 如何检查字符串数组是否在C++中具有换行符
- 是否可以使用字符串::find来检测换行符
- 如果字符串是从字符类型中给定的,则该字符串是否可以包含换行符
- 当使用字符串流解析字符串时,它会提取一个换行符
- 如何检查字符串是否包含空格/制表符/换行符(任何空白内容)?
- C++保留换行符的预处理器字符串化
- 当子字符串只是一个换行符时,string.find(substring) 函数返回什么
- 使用向量按分隔符拆分字符串 - 如何按换行符拆分
- 使用lldb打印带有换行符的字符串