Find exact word in a sentence using Java
java, string
Solution
Try to use word boundary class `\b`
s.matches(".*\\b" + key + "\\b.*")
Problem
I am writing a code to spot country names in the text. I am using a dictionary with names of countries say `India, America, Sri Lanka, ...`. I am currently using `text.contains(key)` with `key` from the dictionary. However, this returns true even for a string like `Indiana`. I tried putting the words of the sentence in an array and then doing the contains, similar approach can be considered with equals but they are really slow. Is there any other faster way you could think of?