Creating substrings array from a string

Viewed 61

I have a string that has no spaces and I wanted to create an array that consists of the substrings of the word. For instance, let the string is stackoverflow The array should be like:

[sta, cko, ver, flo, w]

The code that I use is below and that does give me only the first item. Any help will be appreciated.

public static ArrayList<String> getWords(String s){
    ArrayList<String> words = new ArrayList<String>();
    for(int i=0;i<s.length(); i=i+3){
        words.add(s.substring(i, 3));
    }
    return words;
}
4 Answers
Related