How to find a whole word in a String in java

2019-01-03 06:08发布

I have a String that I have to parse for different keywords. For example, I have the String:

"I will come and meet you at the 123woods"

And my keywords are

'123woods' 'woods'

I should report whenever I have a match and where. Multiple occurrences should also be accounted for. However, for this one, I should get a match only on 123woods, not on woods. This eliminates using String.contains() method. Also, I should be able to have a list/set of keywords and check at the same time for their occurrence. In this example, if I have '123woods' and 'come', I should get two occurrences. Method execution should be somewhat fast on large texts.

My idea is to use StringTokenizer but I am unsure if it will perform well. Any suggestions?

13条回答
爷、活的狠高调
2楼-- · 2019-01-03 07:01

Try to match using regular expressions. Match for "\b123wood\b", \b is a word break.

查看更多
登录 后发表回答