sarath
sarath

Reputation: 455

Java String check

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.

Upvotes: 1

Views: 176

Answers (9)

Reimeus
Reimeus

Reputation: 159754

You could do

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

Upvotes: 0

Florent Bayle
Florent Bayle

Reputation: 11890

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,...

Upvotes: 1

gkrls
gkrls

Reputation: 2664

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

Upvotes: 1

Sreedhar GS
Sreedhar GS

Reputation: 2788

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.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

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

Upvotes: 4

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

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

Upvotes: 0

ZaoTaoBao
ZaoTaoBao

Reputation: 2615

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

Upvotes: 0

Le Ish Man
Le Ish Man

Reputation: 461

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

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

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

        }
    }

Upvotes: 3

Related Questions