0

I want to find out if a string is a precise word, but for example if I do it this way:

String s1="Welcome";
String s ="Welcomes to Tutorialspoint.com";
System.out.println(s.contains(s1));

It returns true, but I want you to give me back false, because in the variable s with the word Welcomes and not the word Welcome of s1. How do I proceed?

6 Answers6

10

You can use

Arrays.asList(s.split("\\s+")).contains(s1)

You can modify this solution to fit your needs by adapting the regex that is used for splitting. For instance, if you wanted to split on anything that isn't a word character (i.e. punctuation and whatnot), you could use \W+.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • His question isn't clear. This approach will work, but not if he wants to ignore punctuation or case. (Sounds like a job for Java 8!) – chrylis -cautiouslyoptimistic- Sep 04 '13 at 14:41
  • @chrylis It's very easy to modify this solution to cover all of that. – arshajii Sep 04 '13 at 14:42
  • What about if I would like to find `word` in `Some simple word. And another "word".`? – Pshemo Sep 04 '13 at 14:44
  • @Pshemo Then you can split on `\W+`. – arshajii Sep 04 '13 at 14:46
  • It might be elegant, but this is only good for very simple cases, will catch you out if you do not understand how it is working, has the same (or greater) overhead as using a matcher (as far as the regex is concerned), creates an Array and a List, performs 3 functions on one line for brevity not clarity and doesn't teach Enzo anything about how someone might really use Java. It is a very popular answer though! – JohnMark13 Sep 04 '13 at 15:00
3

Use `.matches()` with the following regex:

System.out.println(s.matches(".*\\b" + s1 + "\\b.*"));

See this tutorial and this question for more info.

Also, if you want to ignore case, you can do this:

System.out.println(s.toUpperCase().matches(".*\\b" + s1.toUpperCase() + "\\b.*"))
Community
  • 1
  • 1
James Dunn
  • 8,064
  • 13
  • 53
  • 87
2

You can split the sentence and then compare each of the words with s1.

String s1="Welcome";
String s ="Welcomes to Tutorialspoint.com";
String[] split = s.split(" ");
for (String s : split) {
  if (s.equals(s1)) {
    System.out.println(s);
    break; //optional
  }
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
1

You can try

s.matches(".*\\b" + s1 + "\\b.*")

using the "\\b" to find a word boundary.

Menachem
  • 911
  • 7
  • 22
  • This won't work, you will need ".*" before the first "\\b" and after the second "\\b", otherwise it will only return true if s.equals(s1). See http://stackoverflow.com/questions/9464261/how-to-find-the-exact-word-using-a-regex-in-java – James Dunn Sep 04 '13 at 14:51
  • Good point. Only realized that after I saw your post. Regex corrected accordingly. – Menachem Sep 04 '13 at 14:58
0

Use StringTokenizer to tokenize the String into constituent words. Then compare each word with the s1.

Anurag Kapur
  • 675
  • 4
  • 19
  • 4
    StringTokenizer is deprecated. – arshajii Sep 04 '13 at 14:39
  • 2
    Can you post a link to the documentation that says its deprecated please? I think StreamTokenizer is deprecated not StringTokenizer. http://docs.oracle.com/javase/7/docs/api/deprecated-list.html#class – Anurag Kapur Sep 04 '13 at 14:41
  • From the link you posted: *"`StringTokenizer` is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."* – arshajii Sep 04 '13 at 14:48
  • 1
    So does not have `deprecated` word, but nearly. – Aritz Sep 04 '13 at 14:51
  • Thanks. So just to be clear, it is NOT deprecated but discouraged. Cool, didnt know that. Learnt something new today! – Anurag Kapur Sep 04 '13 at 14:51
  • Okay, sure. It's not *officially* considered to be deprecated perhaps, but it's use is certainly discouraged. – arshajii Sep 04 '13 at 14:51
  • 1
    See also: http://stackoverflow.com/questions/6983856/why-is-stringtokenizer-deprecated – arshajii Sep 04 '13 at 15:05
0

For something a little more advanced than using simple String parsing and/or regular expressions you could take a look at how Lucene parses and analyses text.

I suspect its more than you need and it adds a dependency to do something that in your case is quite simple.

pillingworth
  • 3,238
  • 2
  • 24
  • 49