我能想到的唯一方法很复杂 您可以首先获取对字符串进行拆分的组数,然后获取每组中的字符数。 当你有,你就可以开始制作群体,而当亚组的一个超过上限
//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;
}
固定列大小或没有停止? – msarchet 2012-04-16 19:44:07
不,列的大小可以是任意大小,因为间距是为了将它们放在一条线上,并且它不超过换行。只要线上的字符串数量必须相同。除了最后一行可以少一些 – 2012-04-16 19:56:36
您能详细说明每行的字符串限制吗?我想我明白你在说什么,但是我不确定你想要什么,如果你在第一行说5个字符串,并且在下一行需要5个更大的字符串导致它包装,但这违反了“相同规则“。 – covertCoder 2012-04-16 19:57:42