繁体   English   中英

在指定时间间隔之前将一个字符串拆分为一个字符串数组

[英]Splitting a string into a string array at a character before a specified interval

我想在一定的间隔内将一个字符串拆分成一个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拆分为第一个索引。 有什么建议么?

您可以使用此:

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;  
}  

我建议使用WordUtils的Wrap函数在适当的换行点处添加换行符,然后再输入String.split("\\\\r?\\\\n"); 将结果字符串拆分为字符串数组。

尝试使用此代码

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;
}

您可以在此处开始表格并进行改进...

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;
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM