No I know that in order to compare a String you have to call its method .equals but how I check if a String is NOT equal to something? Will it be something like if ("hello" != String.equal())?? or is there a special method?

I know I'm being dumb here lol but after some researching I can't find the answer.

Thanks :)

Recommended Answers

All 16 Replies

two strings are equal in case of equals method if both strings that you are comparing have the same characters and have the same case such as "hello".equals("hello") will return true as both the strings have the same characters h,e,l,l,o and they do have the same length and also have the same case.
Now change the case of h to H i.e write "Hello".equals("hello") this will return false which shows that the strings are not equal.
understood.

commented: More confusing than helpful. -1

String.equal() return type is boolean
example:

String a = "not Hello"
if(a.equal("Hello")){
will do whatever here if result is true}

or

if(!a.equal("Hello")){
will do whatever here if result is true}

and so on..

Use ( ! "hello".equals(someString) ) edit: Yes, same thing striker just posted :)

Use ( ! "hello".equals(someString) ) edit: Yes, same thing striker just posted :)

Thank you VERY MUCH. I didn't find anything on a not equals method for comparing strings. Placing the not operator before the equals test never occured to me. Oh well, such are the pains of beginning programing classes. Thanks again! :):)

Haha good to see an old post is still helping people

can i use like these ?
String a = "Hello";
if ( a == "Hello" )

am php programmer .. never try java.

thx .

Why don't you just try it? Just by reading the thread (I started it a while back now so can't remember) I would say you need the .equals() method to return for you...

Why don't you just try it? Just by reading the thread (I started it a while back now so can't remember) I would say you need the .equals() method to return for you...

thank you for reply
i don't have java install ^^" today i learning java and downloading jdk :twisted:

Yes, I bet the frendly people who help here would wish more people would search for answers before posting duplicate questions. :)

On a side note, I'm going to have to ask for help in a second, since I have a switch statement that just isn't working correctly. :(:(

Yes, I bet the frendly people who help here would wish more people would search for answers before posting duplicate questions. :)

On a side note, I'm going to have to ask for help in a second, since I have a switch statement that just isn't working correctly. :(:(

Agreed. It's even more painful when someone asks "how do you do X" when "how to do X" was just answered in that same thread.

I saw a bunch of those as I was browsing for an answer earlier today. I think it's sad that people will ask for help (AKA your time) and not even be willing to read a thread's posts.

how do l compare strings,and can l name my string like this
String program ="lamp","goat","cow";
if(program.equal(goat))
{
do something
}

Don't hijack threads with new questions. Start a new thread of your own.

And the answer to your second question is no.

Use ! to negate a boolean expression, in this case !str1.equals(str2).
Ex: !guess.equals(password)
Hope this helps!! :) ;)

if(!hello.equals(hello))

this will give you false

if(hello.equals(hello))

this will give you true

if(hello.equals(HELLO))

this will give you false

if(hello.equalsIgnoreCase(HELLO))

will give you true
Hope this helped!!!

if(!hello.equals(hello))

this will give you false

if(hello.equals(hello))

this will give you true

if(hello.equals(HELLO))

this will give you false

if(hello.equalsIgnoreCase(HELLO))

will give you true
Hope this helped!!!

didn't help..
this thread was started (and hijacked) years ago, by people who didn't come back to the forum. I seriously doubt they 'll come back just to read this answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.