Java: How to split a string by a number of characters?

前端 未结 11 1435
夕颜
夕颜 2020-11-27 06:59

I tried to search online to solve this question but I didn\'t found anything.

I wrote the following abstract code to explain what I\'m asking:

String         


        
相关标签:
11条回答
  • 2020-11-27 07:15

    I don't think there's an out-of-the-box solution, but I'd do something like this:

    private String[] splitByNumber(String s, int chunkSize){
        int chunkCount = (s.length() / chunkSize) + (s.length() % chunkSize == 0 ? 0 : 1);
        String[] returnVal = new String[chunkCount];
        for(int i=0;i<chunkCount;i++){
            returnVal[i] = s.substring(i*chunkSize, Math.min((i+1)*chunkSize-1, s.length());
        }
        return returnVal;
    }
    

    Usage would be:

    String[] textArray = splitByNumber(text, 4);
    

    EDIT: the substring actually shouldn't surpass the string length.

    0 讨论(0)
  • 2020-11-27 07:15

    This is the simplest solution i could think off.. try this

    public static String[] splitString(String str) {
        if(str == null) return null;
    
        List<String> list = new ArrayList<String>();
        for(int i=0;i < str.length();i=i+4){
            int endindex = Math.min(i+4,str.length());
            list.add(str.substring(i, endindex));
        }
      return list.toArray(new String[list.size()]);
    }
    
    0 讨论(0)
  • 2020-11-27 07:17

    What about a regexp?

    public static String[] splitByNumber(String str, int size) {
        return (size<1 || str==null) ? null : str.split("(?<=\\G.{"+size+"})");
    }
    

    See Split string to equal length substrings in Java

    0 讨论(0)
  • 2020-11-27 07:17

    Try this

     String text = "how are you?";
        String array[] = text.split(" ");
    

    Or you can use it below

    List<String> list= new ArrayList<String>();
    int index = 0;
    while (index<text.length()) {
        list.add(text.substring(index, Math.min(index+4,text.length()));
        index=index+4;
    }
    
    0 讨论(0)
  • 2020-11-27 07:19

    Using Guava:

    Iterable<String> result = Splitter.fixedLength(4).split("how are you?");
    String[] parts = Iterables.toArray(result, String.class);
    
    0 讨论(0)
  • 2020-11-27 07:19

    My application uses text to speech! Here is my algorithm, to split by "dot" and conconate string if string length less then limit

    String[] text = sentence.split("\\.");
     ArrayList<String> realText =  sentenceSplitterWithCount(text);
    

    Function sentenceSplitterWithCount: (I concanate string lf less than 100 chars lenght, It depends on you)

    private ArrayList<String> sentenceSplitterWithCount(String[] splittedWithDot){
    
            ArrayList<String> newArticleArray = new ArrayList<>();
            String item = "";
            for(String sentence : splittedWithDot){
    
                item += DataManager.setFirstCharCapitalize(sentence)+".";
    
                if(item.length() > 100){
                    newArticleArray.add(item);
                    item = "";
                }
    
            }
    
            for (String a : newArticleArray){
                Log.d("tts", a);
    
            }
    
            return newArticleArray;
        }
    

    function setFirstCharCapitalize just capitalize First letter: I think, you dont need it, anyway

       public static String setFirstCharCapitalize(String input) {
    
    
            if(input.length()>2) {
                String k = checkStringStartWithSpace(input);
                input = k.substring(0, 1).toUpperCase() + k.substring(1).toLowerCase();
            }
    
            return input;
        }
    
    0 讨论(0)
提交回复
热议问题