【问题标题】:Splitting a string into a string array at a character before a specified interval在指定间隔之前的一个字符处将字符串拆分为字符串数组
【发布时间】:2014-08-07 16:19:53
【问题描述】:

我希望在一个区间内将一个字符串拆分为一个字符串数组,但我不希望将单词切成两半。例如:

说文本是:

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 拆分为第一个索引。有什么建议吗?

【问题讨论】:

    标签: java arrays string split


    【解决方案1】:

    您可以从这里开始并改进它...

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

    }

    【讨论】:

      【解决方案2】:

      尝试使用此代码

      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】:

        我建议使用WordUtils's Wrap function 在适当的换行点添加换行符,然后使用String.split("\\r?\\n"); 将生成的字符串拆分为字符串数组。

        【讨论】:

        • 漂亮,正是我想要的,谢谢。
        【解决方案4】:

        你可以用这个:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-27
          • 1970-01-01
          • 1970-01-01
          • 2012-04-08
          • 1970-01-01
          • 2015-12-28
          • 2011-08-22
          • 2014-06-29
          相关资源
          最近更新 更多