1.11M Members

String.equals()

 
0
 

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

 
-1
 

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.

 
0
 

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

 
0
 

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

 
0
 

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! :):)

 
0
 

Haha good to see an old post is still helping people

 
0
 

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

am php programmer .. never try java.

thx .

 
0
 

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

 
0
 

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:

 
0
 

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. :(:(

 
0
 

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.

 
1
 

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.

 
0
 

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

 
0
 

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

And the answer to your second question is no.

 
0
 

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

 
-1
 
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!!!

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

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article