1

i am checking whether "of" available in a sentence or not.I am trying to do a check on java String. page is a String variable and the code is here

page="Paint, Body & Trim : Body : Moldings : Sunroof";



if(page.contains("of"))
{
}
else
{
}

the problem in the above example sunroof has "of" so the loop give true. but i dont want words with "of" to be taken . please help me with that.

9 Answers 9

4

Just change this

if(page.contains("of"))

to

if(page.contains(" of "))

EDIT: Just to consider if the sentence starts or finishes with "of":

if(page.contains(" of ") || page.startsWith("of ") || page.endsWith(" of"))
Sign up to request clarification or add additional context in comments.

3 Comments

this will fail if "of" is at start of string i.e no string at start.
This will fail if "of" is before a punctuation sign.
@ZaheerAhmed How come will it return true Sunroof and" as page.startsWith("of ")??
3

You can do it in this way. use equals() instead of contains()

String page="Paint, Body & Trim : Body : Moldings : Sunroof";
  for (String i:page.split(" ")){
        if("of".equals(i)){

        }
    }

Comments

1

You can use Scanner with " : " delimiter:

String page="Paint, Body & Trim : Body : Moldings : Sunroof";

boolean contains(Scanner scan, word){
    scan.useDelimiter(" : ");
    while(in.hasNext())
        if(in.next().equals(word) return true;
    return false;
}

and then make a call like this System.out.println(contains(new Scanner(page), "of");

This is print false

Comments

1

Try if(page.contains(" of ")) that way it will only take the word "of" and not strings with that substring.

Comments

1

The best way to do it is to use a regexp :

page.matches(".*\\b[Oo][Ff]\\b.*")

.* means "any char zero or more times". \\b is a word boundary. [Oo] means the character 'O', upper case or lower case.

Here are some test cases:

String page = "Paint, Body & Trim : Body : Moldings : Sunroof";
System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // false
page = "A piece of cake";
System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
page = "What I'm made of";
System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
page = "What I'm made of.";
System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
page = "What I'm made of, flesh";
System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
page = "Of the Night";
System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true

Matching " of " (with spaces before and after) will not work in the cases where "of" is at the beginning, at the end, before a punctuation,...

Comments

0

or another way if you just look whole of word:

String page="Paint, Body & Trim : Body : Moldings : Sunroof";
StringTokenizer st2 = new StringTokenizer(str, " ");//space

        while (st2.hasMoreElements()) {
            if((st2.nextElement().equals("of")){
              //whatever..
             }
        }

Comments

0

You need to use regex here it would make it easy else you need to add many checks:

System.out.println(str.matches(".*\\bof\\b.*"));   //will return true

Here is Demo

Comments

0

Add spaces before and after the search string, i.e

if(page.contains(" of ")){
}

Other way is splitting sentence with space delimeter, but here you need to loop throgh the string array.

Comments

0

You could do

if (page.matches(".*\\bof\\b.*")) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.