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.

7
  • 2
    You could use the string.split method and then check if the resulting array contains your word.
    – takendarkk
    Apr 30, 2014 at 4: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;. Apr 30, 2014 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. Apr 30, 2014 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 1, 2014 at 7:21
  • See also stackoverflow.com/questions/15480811/…
    – Raedwald
    May 1, 2014 at 7:31

12 Answers 12

40

try regex

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

\b means word boundary

7
  • 4
    +1 for using word boundary (and you can throw away the .*)
    – Salman A
    Apr 30, 2014 at 9:45
  • 2
    IIRC, the entirety of s must match the given regular expression in Java, so the .* must be kept.
    – Teepeemm
    Apr 30, 2014 at 16:08
  • Wouldn't it be more technically correct to use a whitespace tokenizer to solve this problem? Apr 30, 2014 at 20:33
  • @Fuser97381 A whitespace tokenizer would fail on the example sentence when searching for a without additional pre/post-processing (due to the comma)
    – Izkata
    Apr 30, 2014 at 20:37
  • 4
    @SalmanA in java, you must keep the ".*" either end
    – Bohemian
    May 1, 2014 at 3:16
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

4
  • What if the OP wants to search the word with case insensitive
    – Gundamaiah
    Apr 30, 2014 at 4:56
  • if OP wants I will suggest him stackoverflow.com/a/15269846/821057 Apr 30, 2014 at 5:02
  • And the comma in the OP's example (if they were searching for a)?
    – Izkata
    Apr 30, 2014 at 20:41
  • Wouldn't splitting by word boundaries also return between-words-sequences?
    – Bergi
    May 1, 2014 at 4:52
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.

6
  • 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, 2014 at 8:40
  • As @TimB mentioned, String.split 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, 2014 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, 2014 at 18:57
  • You should not be using an or (|) within a character class. That is not how character classes work.
    – Bob
    Apr 30, 2014 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 1, 2014 at 4:51
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/

3
  • 1
    This will not work for entire sentences. String.matches() only returns true if the entire string matches.
    – user3490218
    Apr 30, 2014 at 4: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, 2014 at 4: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)
    – user3490218
    Apr 30, 2014 at 4: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.

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]);
            }

        }
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) {

3
  • 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, 2014 at 4: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. Apr 30, 2014 at 5:07
  • 1
    Change to (" " + sentence + " ").indexOf..
    – Salman A
    Apr 30, 2014 at 9: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;
    }
}
2
  • This won't work if there is punctuation after str1.
    – Teepeemm
    Apr 30, 2014 at 16:05
  • You might be right. I hadn't even thought of that. Let me see if I can find a workaround.
    – Tina T
    May 2, 2014 at 3:45
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.

2
  • -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. Apr 30, 2014 at 8: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. Apr 30, 2014 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;
}

}
9
  • How would it work for I am Karthick, you are not - search for Karthick Apr 30, 2014 at 5:13
  • Sorry.. I don't get you. Apr 30, 2014 at 5:18
  • based upon your code you will get the following tokens I am Karthick, you are not Apr 30, 2014 at 5:19
  • I have tested all three words in my sentence and got correct answer Apr 30, 2014 at 5:20
  • so change the sentence to I am Karthick, you are not and try again. Apr 30, 2014 at 5:20
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.

6
  • 2
    what if word at start of sentence than it wont have space at start similarly for end. Apr 30, 2014 at 4:39
  • This will not work If the word will be the first word in the sentence
    – Gundamaiah
    Apr 30, 2014 at 4:40
  • Hlo. suppose my word is "I" then what would be your answer? Apr 30, 2014 at 4:43
  • what if I want to search for a in above sentence I am a, Java Programmer. As per your code it will return False as after index + 1 you will see a comma, wouldn't it? Apr 30, 2014 at 5:05
  • Sure, if you want to update your question to include this, then I will simply add code to check index + 1 is not between 0-9 and a-Z. Apr 30, 2014 at 5:09
-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 + " ";

4
  • I did not vote down, but this answer is wrong. Apr 30, 2014 at 4: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, 2014 at 4:18
  • 1
    Probably because it fails the example in the question: isPresent("ram", "I am a, Java Programmer.") is true but should be false. Apr 30, 2014 at 4:18
  • I didn't downvote, but this suggestion is logically equivalent to the OP's existing code.
    – jahroy
    Apr 30, 2014 at 4:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.