How to find a whole word in a String in Java?

230,937

Solution 1

The example below is based on your comments. It uses a List of keywords, which will be searched in a given String using word boundaries. It uses StringUtils from Apache Commons Lang to build the regular expression and print the matched groups.

String text = "I will come and meet you at the woods 123woods and all the woods";

List<String> tokens = new ArrayList<String>();
tokens.add("123woods");
tokens.add("woods");

String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);

while (matcher.find()) {
    System.out.println(matcher.group(1));
}

If you are looking for more performance, you could have a look at StringSearch: high-performance pattern matching algorithms in Java.

Solution 2

Use regex + word boundaries as others answered.

"I will come and meet you at the 123woods".matches(".*\\b123woods\\b.*");

will be true.

"I will come and meet you at the 123woods".matches(".*\\bwoods\\b.*");

will be false.

Solution 3

Hope this works for you:

String string = "I will come and meet you at the 123woods";
String keyword = "123woods";

Boolean found = Arrays.asList(string.split(" ")).contains(keyword);
if(found){
      System.out.println("Keyword matched the string");
}

http://codigounico.blogspot.com/

Solution 4

How about something like Arrays.asList(String.split(" ")).contains("xx")?

See String.split() and How can I test if an array contains a certain value.

Solution 5

public class FindTextInLine {
    String match = "123woods";
    String text = "I will come and meet you at the 123woods";

    public void findText () {
        if (text.contains(match)) {
            System.out.println("Keyword matched the string" );
        }
    }
}
Share:
230,937
Nikola Yovchev
Author by

Nikola Yovchev

Most managers may defend the schedule and requirements with passion; but that’s their job. It’s your job to defend the code with equal passion. Robert Martin (Uncle Bob), Clean code The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. Tom Cargil, Bell labs My job really is, in the end, is to say 'no.' Somebody has to say 'no' to [this patch or that pull request]. And because developers know that if they do something that I'll say 'no' to, they do a better job of writing the code. Linus Torvalds All problems in computer science can be solved by another level of indirection..., except for the problem of too many layers of indirection. David Wheeler If privacy is outlawed, only outlaws will have privacy. Philip Zimmermann

Updated on July 30, 2021

Comments

  • Nikola Yovchev
    Nikola Yovchev over 2 years

    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?