33

I am new to programming and working on a function to return true if a word is present in a sentence. I tried the indexOf() method, but then I also came across a certain issue with this approach:

Suppose my sentence is I am a, Java Programmer.

If we look for the word ram with the indexOf() method then it will return true because ram is present in Programmer while the correct output should be false as ram is not present as a word but as a pattern.

How can I work around this problem? The code that I am using as of now is:

boolean isPresent(String word, String sentence)
{
    if(sentence.indexOf(word) >= 0)
        return true;
    else
        return false;
}

NOTE: The word ram is just an example to show one of the issue with my current approach.It's not that I have to work with ram only all the time.The word can be any like say a which is followed by a comma in above sentence.

UPDATE: Thanks everyone for providing their comments and solutions. I have selected one as an accepted answer(would have selected more if permitted :-)), but a lot were helpful.

Sebastian
  • 6,293
  • 6
  • 34
  • 47
user2966197
  • 2,793
  • 10
  • 45
  • 77
  • 2
    You could use the string.split method and then check if the resulting array contains your word. – takendarkk Apr 30 '14 at 04:11
  • 3
    By the way, you can simplify that function's definition like this: `return sentence.indexOf(word) >= 0;` - `sentence.indexOf(word) >= 0` is either `true` or `false`, so depending on the situation, this either means `return true;` or `return false;`. – Panzercrisis Apr 30 '14 at 14:44
  • The question is clear and complete, but I can find the answer to this question multiple times throughout Stackoverflow and and many other places. – Abu Sulaiman Apr 30 '14 at 20:41
  • @AbuSulaiman in that case, you should vote to close this queztion as a duplicate, rather than just keaving a comment. – Raedwald May 01 '14 at 07:21
  • See also http://stackoverflow.com/questions/15480811/for-loop-to-search-for-word-in-string – Raedwald May 01 '14 at 07:31
  • See also http://stackoverflow.com/questions/3879160/java-string-search – Raedwald May 01 '14 at 07:39
  • See also http://stackoverflow.com/questions/15779632/find-exact-word-in-a-sentence-using-java – Raedwald May 01 '14 at 07:47

12 Answers12

40

try regex

boolean contains = s.matches(".*\\bram\\b.*");

\b means word boundary

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
21

Since you want to search a word there are three cases:

  1. word at start of sentence means no space at start but space at end.
  2. word between the sentence space at both ends.
  3. word at end only space at end.

To cover all the three cases, one possible solution is:

String str = "I am a JAVA programmer";
String[] splited = str.split("\\b+"); //split on word boundries
Arrays.asList(splited).contains("ram"); //search array for word

Here is working Demo

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
10

Question:

How do you define a word?

Possible answer:

Bunch of characters separated by some other characters. This second set of characters is defined by what you choose. Suppose you choose these to be . ,?;. So if you split the input string with these characters (called delimiters), you'll get a bunch of string which are words. Now to find if the input contains the word or not, loop over these strings to check if they match your query.

Code:

boolean isPresent(String query, String s) {    
    String [] deli = s.split("[.\\s,?;]+");

    for(int i=0;i<deli.length;i++)
        if(query.equals(deli[i]))
            return true;

    return false;    
}

tl;dr:

If you want a word to be defined as anything which consists of alphabets, numbers and underscore, there is a regex ready for you: \W+.

String [] deli = s.split("\\W+");

Consider reading this article if you want to learn more about Java Regex.

HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
  • 3
    Good answer, however note that split accepts regex. So you can just split on white space, or not alpha, or whatever. Also you can do equalsIgnoreCase to match capitalized words. – Tim B Apr 30 '14 at 08:40
  • As @TimB mentioned, [`String.split`](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-) takes a regex string. That means two things. Firstly, you can't provide it a list of characters like that (it'll try to use the *whole string* as a regex pattern matching a *single boundary*), though you could use a regex character class (`"[. ,?;]"`). Secondly, any regex metacharacters must be escaped - your use of `.` would match any single character, and `?` would make the `,` optional. – Bob Apr 30 '14 at 17:45
  • While this answer is good in that it helps to get the reader thinking about what needs to happen, it's important to note that `\\b` is an available metacharacter to indicate **word boundaries**, which is exactly the sort of thing the OP is looking for. Be sure to differentiate between `\\b` (word boundary) and `\b` (backspace), though! – Brian S Apr 30 '14 at 18:57
  • You should not be using an or (`|`) within a character class. That is not how character classes work. – Bob Apr 30 '14 at 23:13
  • 2
    Wouldn't splitting by the word boundaries also return between-words-sequences? I had reather expected `s.split("\\W+")` (though that might yield empty strings, not sure how the methods work in java) – Bergi May 01 '14 at 04:51
  • @Bergi You're absolutely right. I am not that much expertised in Java to know that `\W+` existed. Thanks! – HelloWorld123456789 May 01 '14 at 05:10
2

Take a look at the String.matches() method. If you construct the regular expression properly it should be able to do what you want it to. A good place to start for regular expressions would be the Java tutorials: http://docs.oracle.com/javase/tutorial/essential/regex/

awksp
  • 11,764
  • 4
  • 37
  • 44
  • 1
    This will not work for entire sentences. String.matches() only returns `true` if the entire string matches. –  Apr 30 '14 at 04:18
  • So you can do something like `"*\\s+" + word + "\\s+*"` as your regex, right? That should work for sentences... (probably isn't right syntax, but that's the general idea) – awksp Apr 30 '14 at 04:19
  • 1
    Yes. That would (sort of) work. I would also suggest looking up `Pattern` and `Matcher` objects on the Java documentation. Both are classes used for regex string matching, the former for the pattern generation, and the latter for the searching. (Obviously) –  Apr 30 '14 at 04:21
2

If you want to match a word in a sentence even with punctuation, you'll need a regex like this:

  static boolean matchesWord(String toMatch, String matchIn) {
     return Pattern.matches(".*([^A-Za-z]|^)"+toMatch+"([^A-Za-z]|$).*", matchIn);
  }

(You could use \W, but that doesn't count underscores as punctuation.)

Just concatenating spaces onto the beginning and the end won't match, for example, the word "programmer" in the string "I am a Java programmer" because there's no space at the end. It also won't match words directly before or after punctuation.

cassowary
  • 21
  • 5
1
String s="I am a JAVA programmer";
    String s1="JAVA";
    String []p=s.split("\\s*(=>|,|\\s)\\s*");
        for(int i=0;i<p.length;i++)
        {
            if(s1.equals(p[i]))
            {
                System.out.println(p[i]);
            }

        }
learner
  • 3,092
  • 2
  • 21
  • 33
1

A more simpler approach is: if you consider that a word is something like

"My pc there is ram memory" (between spaces)

you could concat into your indexOf function spaces before and after the word that you are searching, like this

if (sentence.indexOf(" "+ word +" ") >= 0) {

jhonis.souza
  • 144
  • 7
  • 2
    Hi @jhonis.souza, welcome to StackOverflow. You might want to consider cases where the word to be found is the first or last in the sentence - for example, how would you handle the case where someone is trying to find the word *Hello* in the sentence *Hello world*, and there is no leading space? – cf- Apr 30 '14 at 04:42
  • Yes, you are correct! The way I tried to use here was not the more assertive, the correct way is use the split or regex, I just tried to show the simpler work around for it or another way to think in the problem, haha. Thanks for correction. – jhonis.souza Apr 30 '14 at 05:07
  • 1
    Change to `(" " + sentence + " ").indexOf..` – Salman A Apr 30 '14 at 09:44
1

This will work, assuming that each word is separated by a space. I've added the main function for clarity. The find_str returns -1 if the word doesn't exist. otherwise, it returns the position of the word with respect to other words. Here, 2 will be returned, meaning that the second word is 'am'.

import java.util.*;
public class HelloWorld{

    public static void main(String []args){
        String str="I am a Java Programmer";
        String str1="am";
        int x=find_str(str,str1);
        System.out.println(x);

    }

    public static int find_str(String main,String search) {

        int i; 
        int found=-1;

        String[] s=main.split(" ");
        for(i=0;i<s.length;i++)
        {
            if(search.equals(s[i]))
            found=i+1;
        }
        return found;
    }
}
Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
Tina T
  • 135
  • 8
0

Try this solution

    int index = sent.indexOf(find);
    if (index != -1) {
        if (index == 0) {
            System.out.println("true");
        }
        else if (index + find.length() == sent.length())
        {
            System.out.println("true");
        }
        else if (sent.charAt(index - 1) == ' ' && sent.charAt(find.length() + index) == ' ') {
            System.out.println("true");
        } else {
            System.out.println("false");
        }

    } else {
        System.out.println("false");
    }

If you want something more than you original question then instead for checking for spaces you should check that they are not between 0-9 and a-Z, this should cover any characters such as comma period etc.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

This is a rather clumsy workaround, but should achieve the right results. Find the substring you are looking for within the string, and find the charachters before and after your substring. Check these using their ascii values (int)substring.charAt(x);to see if they are letters or not. If they are both eithr not letters, or fall outside the bounds of the string, you know you have found a word. Otherwise, you know that it is simply part of a word.

The logic will be very long- which is why I am not coding it for you, but give this a shot and let me know if you need clarification.

  • -1 for recommending the use of ASCII values. We're well past the period of time where it's acceptable for code not to be Unicode-aware. – Chris Hayes Apr 30 '14 at 08:28
  • I used ASCII for this because when you cast a char to an int, the default returned value is the ASCII value. This is inherent to java. – joepeacock001 Apr 30 '14 at 14:53
0

Hlo. You can split the sentence as array and then you put into List. after that you can use contains method to check you word is present or not. Kindly try this code..

import java.util.ArrayList;
import java.util.Arrays;


 public class karthitest {
  public static void main(String[] args) {
    String sentence = "I am Karthick";
    String word = "I";

    if(isWordExist(sentence, word)){
    System.out.println("Word is exist");
    }
}

public static boolean isWordExist(String sentence, String word){
    boolean ans = Boolean.FALSE;        
    ArrayList<String> wordList = null;

    try {

        if(sentence != null && word != null){
            wordList = new ArrayList<String>(Arrays.asList(sentence.split("[^a-zA-z]+")));              
            if(wordList.contains(word)){
                ans = Boolean.TRUE;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        // TODO: handle exception
    }
    return ans;
}

}
Karthikeyan Sukkoor
  • 968
  • 2
  • 6
  • 24
-4

use contains method

boolean isPresent(String word, String sentence)
{
return sentence.contains(word);   
}

EDIT: if you want to search for a pariticular word then you can add space befaore and after the word string
word = " " + word + " ";

ashishmaurya
  • 1,196
  • 10
  • 18
  • I did not vote down, but this answer is wrong. – user1803551 Apr 30 '14 at 04:18
  • 2
    `String.contains()` just returns whether the argument shows up as a substring in the string it's called on; thus, it doesn't work for the poster's situation because he wants whatever method is used to return true only if the argument shows up as an independent word, not as a substring that might be inside another word. – awksp Apr 30 '14 at 04:18
  • 1
    Probably because it fails the example in the question: `isPresent("ram", "I am a, Java Programmer.")` is true but should be false. – Andrew Marshall Apr 30 '14 at 04:18
  • I didn't downvote, but this suggestion is logically equivalent to the OP's existing code. – jahroy Apr 30 '14 at 04:24