Get first character of each word and its position in a sentence/paragraph

Posted by Radhika on Stack Overflow See other posts from Stack Overflow or by Radhika
Published on 2010-05-11T07:27:13Z Indexed on 2010/05/11 7:34 UTC
Read the original article Hit count: 223

Filed under:
|

I am trying to create a map by taking the first character of each word and it's position in a sentence/paragraph. I am using regex pattern to achieve this. Regex is a costly operation. Are there are any ways to achieve this?

Regex way:

public static void getFirstChar(String paragraph) {
    Pattern pattern = Pattern.compile("(?<=\\b)[a-zA-Z]");
    Map newMap = new HashMap();

    Matcher fit = pattern.matcher(paragraph);
    while (fit.find()) {
        newMap.put((fit.group().toString().charAt(0)), fit.start());
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about regex