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

前端 未结 11 1441
夕颜
夕颜 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:28

    I think that what he wants is to have a string split into substrings of size 4. Then I would do this in a loop:

    List strings = new ArrayList();
    int index = 0;
    while (index < text.length()) {
        strings.add(text.substring(index, Math.min(index + 4,text.length())));
        index += 4;
    }
    

提交回复
热议问题