Get first character of each word and its position in a sentence/paragraph
- by Radhika
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());
}
}