如何在每次达到30个字符时拆分字符串

时间:2018-03-30 09:17:57

标签: c++

如何拆分字符串并在每次达到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] <<" ";
    }   

}

1 个答案:

答案 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;