我能想到的唯一方法很复杂,您可以首先获取对字符串进行拆分的组数,然后获取每个组中的字符数。当你拥有它时,你可以开始制作组,当其中一个子组超过限制时你停止
//creates groups and lengths arrays
string letters = aaa bbbb ccc dd eeeeeee ffffffffff gggggggggggggggg hh iiiiiiiiii;
string[] groups = letters.split(" ");
int[] lengths = new int[groups.length]
for(int i = 0; i<groups.length, i++){
lengths[i] = groups[i].length;
}
int numberofrows;
//starts doing groups: is one element is more that 20, then if 2 elements are more
//than 20, 3 elements more than 20....
// when a sum is more than 20, then the last amount of groups was the one to use
for(int i = 0, i<groups.length<i++){
for(int j = 1, j<=groups.length, j++)
int sum = add(i, j)
if(sum > limit){
numberofrows = j-1;
return;
}
}
}
//this makes the addition of several elements of the array, next to the other
public int add(int index, id sums){
int sum = lengths[index];
for(int i = 0, i<=sums;i++){
sum += lengths[index+i];
}
return sum;
}