我能想到的唯一方法很複雜 您可以首先獲取對字符串進行拆分的組數,然後獲取每組中的字符數。 當你有,你就可以開始製作羣體,而當亞組的一個超過上限
//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