我希望在一段时间内将一个字符串拆分成一个字符串数组,但我不希望将这些字词切成两半。例如:
说文字是:
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拆分为第一个索引。有什么建议吗?
答案 0 :(得分:0)
您可以使用:
public static String[] splitByLength(String s, int chunkSize)
{
int arraySize = (int) Math.ceil((double) s.length() / chunkSize);
String[] returnArray = new String[arraySize];
int index = 0;
for(int i = 0; i < s.length(); i = i+chunkSize)
{
if(s.length() - i < chunkSize)
{
returnArray[index++] = s.substring(i);
}
else
{
returnArray[index++] = s.substring(i, i+chunkSize);
}
}
return returnArray;
}
答案 1 :(得分:0)
我建议使用WordUtils's Wrap function在适当的包装点添加换行符,然后String.split("\\r?\\n");
将结果字符串拆分为字符串数组。
答案 2 :(得分:0)
尝试使用此代码
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; ) {
if (splitText[i] == ' ') {
indexOfSpace = i;
}
pos++;
i++;
if (pos == interval) {
message[messageCursor] = text.substring(previousIndex, indexOfSpace+1);
previousIndex = indexOfSpace+1;
pos=pos-message[messageCursor].length();
messageCursor++;
}
else if(i==splitText.length-1)
{
message[messageCursor] = text.substring(previousIndex, text.length());
break;
}
}
return message;
}
答案 3 :(得分:0)
你可以在这里开始形式并改进它......
public class StringSplitter {
public static void main(String args[]){
int radix=50;
String test="Lorem Ipsum Sub Rosa Dolores Clairborne. The quick brown fox zipped quickly to the tall yellow fence and hopped to the other side.";
int arraySize = (int) Math.ceil((double) test.length() / radix);
String[] returnArray = new String[arraySize];
String temp;
int index=0;
while( (temp=split(test, radix))!=null){
returnArray[index]=temp;
index++;
test=test.substring(radix);
}
returnArray[index]=test.trim();
for(String a : returnArray) System.out.println(a);
}
private static String split(String test, int radix){
if(test.length() <= radix) return null;
String s=test.substring(0, radix);
radix++;
while(!s.endsWith(" ") || test.length() <= radix){
s=test.substring(0, radix);
radix++;
}
return s;
}
}