I have Set of character delimiters (DELIMITERS), eg . ,  etc. Using this I want to split text and get words with their position in text. String.split() works fine if you want only words. The same with StringTokenizer. Wrote some simple method to deal with this, but maybe there is a better way to achieve this result?public List<String> extractWords(String text){    List<String> words = new ArrayList<>();    List<WordPos> positions = new ArrayList<>();    int wordStart = -1;    for(int i=0; i < text.length(); i++){        if(DELIMITERS.contains(text.charAt(i))){            if(wordStart >=0){ //word just ended                String word = text.substring(wordStart, i);                positions.add(new WordPos(wordStart, i));                words.add(word);            }            wordStart = -1;        }else{ //not delimiter == valid word            if(wordStart < 0){ //word just started                wordStart = i;            }        }    }    return words;}// inner static class for words positionspublic static class WordPos{    int start;    int end;    public WordPos(int start, int end){        this.start = start;        this.end = end;    }}
Text tokenizer - extract words and positions from text
java;strings