2014-06-17 65 views
0

我在找一個字符串分割成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分解成第一個索引。有什麼建議麼?

回答

0

我會建議使用WordUtils's Wrap function添加換行符在適當的包裝點,然後String.split("\\r?\\n");所得到的字符串分割成字符串數組。

+1

美麗的,正是我一直在尋找,謝謝。 – Dan

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

}