簡體   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