Question

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.

Was it helpful?

Solution

try regex

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

\b means word boundary

OTHER TIPS

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

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.

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/

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.

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

        }

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

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

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.

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

}

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.

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 + " ";

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top