如何拆分字符串并在每次达到30个字符时将其移动到下一行?我试着在这里搜索,但大部分都让我难以理解。
#include<iostream>
#include<string>
using namespace std;
int main(){
string arr;
cout<<"Enter Sentence: ";
getline(cin,arr);
for(int i = 0; i<30;i++){
cout<<i <<" ";
}
cout<<endl;
for(int j = 0; j<arr.length(); j++){
cout<<arr[j] <<" ";
}
}
答案 0 :(得分:0)
也许substr
可以帮到你。例如:
vector<string> result;
string input;
int split=0;
int size=string.size();
while(split+30<size)
{
result.push_back(input.substr(split,30));
split+=30;
}
result.push_back(input.substr(split,size-split+1));
//and you can do anything you want on the list of results;