Find a complete word in a string java

前端 未结 9 2044
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 06:52

I am writing a piece of code in which i have to find only complete words for example if i have

String str = \"today is tuesday\";

and I\'m

9条回答
  •  被撕碎了的回忆
    2021-01-16 07:04

    I use a regexps for such tasks. In your case it should look something like this:

    String str = "today is tuesday";
    return str.matches(".*?\\bt\\b.*?"); // returns "false"
    
    String str = "today is t uesday";
    return str.matches(".*?\\bt\\b.*?"); // returns "true"
    

    A short explanation:

    . matches any character, *? is for zero or more times, \b is a word boundary.

    More information on regexps can be found here or specifically for java here

提交回复
热议问题