Split a String to a String[] so that each element is maximum 100 characters and ends in a space

前端 未结 4 997
囚心锁ツ
囚心锁ツ 2021-01-28 16:53

How do i do this? I tried something like this but I do not seem to be able to get any further.

public void speak(String text)
{
    String[] textArray = text.spl         


        
4条回答
  •  旧时难觅i
    2021-01-28 17:15

    Is it possible with regular expression, it needs less code:

    Pattern p=Pattern.compile("[\\s\\S]{99}");
    Matcher matcher = p.matcher(s);
    ArrayList list= new ArrayList<>();
    
    int lastFound=0;
    while (matcher.find()){
        list.add(matcher.group()+" ");
        lastFound = matcher.end();      
    }
    list.add(s.substring(lastFound));
    

    If you mean something else just comment! May be you need to add more patterns in your string has tabs, new line and other special chars.

提交回复
热议问题