我在找一個字符串分割成String數組在時間間隔,但我不想在半被削減的言論。例如:將字符串分割成字符的字符串數組指定的時間間隔
說文字是:
Lorem Ipsum Sub Rosa Dolores Clairborne. The quick brown fox zipped quickly to the tall yellow fence and hopped to the other side.
Interval: 50
將分爲以下
separated[0]: "Lorem Ipsum Sub Rosa Dolores Clairborne. The quick brown"
separated[1]: "fox zipped quickly to the tall yellow fence and"
separated[2]: "hopped to the other side."
我一直在試圖弄清楚這一點,最好的我想出來的是:
private static String[] splitMessage(String text, int interval) {
char[] splitText = text.toCharArray();
String[] message = new String[(int) Math.ceil(((text.length()/(double)interval)))];
int indexOfSpace = 0, previousIndex = 0, messageCursor = 0, pos = 0;
for (int i = 0; i < splitText.length-1; i++) {
if (splitText[i] == ' ') {
indexOfSpace = i;
}
if (pos == interval) {
message[messageCursor] = text.substring(previousIndex, indexOfSpace);
previousIndex = indexOfSpace;
messageCursor++;
}
pos++;
}
return message;
}
這最終只將Lorem分解成第一個索引。有什麼建議麼?
美麗的,正是我一直在尋找,謝謝。 – Dan