tags:

views:

105

answers:

6

Hi everyone,

I want to ask if anyone knows whether Java has built in library for doing something like the following.

For instance,

I have 2 Strings which are: String a = "Yeahh, I love Java programming."; String b = "love";

I want to check whether the String b which contains "love" is part of the tokens in the String a. Hence, I want to ask are there any Java API to do so? I want something like,

a.contains (b) <--------- Return true result

Are there any??? because,,,,if there's no Java API for that, I would write my own algorithm then.

Thanks in advance for any helps..^^

A: 

You can use the indexOf() function in the String library to check for it:

if(a.indexOf(b)>=0) 
return true;
Corazu
Err, there's a contains(...) in String for a couple of years!
Bart Kiers
And if doing something like `if(boolead) return boolean;` mind as well do `return boolean;`. In other words: `return a.indexOf(b) >= 0`
Bart Kiers
Of course, it's just a matter of styling.
Corazu
Not in this case. The above method body does not have a return path for when `a` does *not* contain `b` and is thus pretty invalid.
Bombe
Thanks Corazu for the idea. I appreciate for the idea given. :)
Mr CooL
A: 

Yes, there is: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#contains(java.lang.CharSequence).

Bart Kiers
Thanks Bart,,I just knew it. haha...:)
Mr CooL
+1  A: 
string strToCheck = "check me";    
int firstOccurence = strToCheck .indexOf("me");
//0 if no any
Restuta
Hey, Restuta,thanks for the info,,,,But then, if putting strToCheck .indexOf("check"); will return 0 result also.But, you really reminded me of this indexOf method. Thanks a lot. :)
Mr CooL
Nice to help you, cheers =)
Restuta
+1  A: 

the method contains(CharSequence s) exist in the class String.

So your a.contains(b) will work

Nettogrof
haha...Nettogrof,,,I didn't know the CharSequence accepts String even though I knew there's this method.Really thanks for highlighting that. Appreciate very much. :)
Mr CooL
+9  A: 

The suggestions so far (indexOf, contains) are all fine if you just want to find substrings. However, given the title of your question, I assume you actually want to find words. For instance, if asked whether "She wore black gloves" contained "love" my guess is you'd want the answer to be no.

Regular expressions are probably the best way forward here, using a word boundary around the word in question:

import java.util.regex.*;

public class Test
{    
    public static void main(String[] args)
    {
        System.out.println(containsWord("I love Java", "love"));
        System.out.println(containsWord("She wore gloves", "love"));
        System.out.println(containsWord("start match", "start"));
        System.out.println(containsWord("match at end", "end"));
    }

    public static boolean containsWord(String input, String word)
    {
        Pattern pattern = Pattern.compile("\\b" + Pattern.quote(word) + "\\b");
        return pattern.matcher(input).find();
    }
}

Output:

true
false
true
true
Jon Skeet
Hey Jon Skeet,Thanks a lot man..I appreciate very much for your help.I'm currently doing my final Computer Science project, Plagiarism Analyzer which I need to do web crawler and so on.The code given by you can be a good start for me to work on the text analysis.Thanks once again. :)
Mr CooL
btw, Jon Skeet..I've not really attempted Pattern usage. For saving my time,Can I ask you whether something like,containsWords( "I love Java", "I Java") <----Will return true alsoCan it be done using Pattern object? If can, I would like to explore more for Pattern object manipulation. Thanks..:)
Mr CooL
No, that will return false. You may well be able to fix it with a more complex regular expression, but I don't have the time to try to come up with it right now.
Jon Skeet
A: 

If you want to find the words will be quite easy, you just have to use split.

//Split to an array using space as delimiter
    String[] arrayOfWords = a.split(" ");

And then you'll compare like:

"love".equalsIgnoreCase(arrayOfWords[i]);

something like that, you get the idea

MexicanHacker
That wouldn't match "Yeahh" in this case because of the comma. You'd need to split on word boundaries instead of just space.
Jon Skeet
split has an "any whitespace" operator, and as well is this supposed to be scalable and / or is it learning and for skill improvement. The reason I as this is some sort of word count in a basic skill that is needed again and again. David Eck has an especially good implementation, the apostrophe 's' on the end of some words makes for really challenging work. There are numerous data structures that can do what you want in different ways. Start with HashMap or something, work till you are stuck, let us know.
Nicholas Jordan
Wow, thanks MexicanHacker for idea for another manipulation of String method and also Jon Skeet and Nicholas Jordan for highlighting important issues. :)
Mr CooL
Oh ya, to share something I know, to split it in such way can be done in StringTokenizer also..:)
Mr CooL