954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String.equals()

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

eddy556
Light Poster
32 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 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..

striker3344
Newbie Poster
19 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
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! :):)

Chad W
Newbie Poster
5 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Haha good to see an old post is still helping people

eddy556
Light Poster
32 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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

am php programmer .. never try java.

thx .

truebot
Newbie Poster
11 posts since Oct 2007
Reputation Points: 10
Solved Threads: 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...

eddy556
Light Poster
32 posts since Jan 2008
Reputation Points: 10
Solved Threads: 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:

truebot
Newbie Poster
11 posts since Oct 2007
Reputation Points: 10
Solved Threads: 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. :(:(

Chad W
Newbie Poster
5 posts since Oct 2009
Reputation Points: 10
Solved Threads: 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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

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.

Chad W
Newbie Poster
5 posts since Oct 2009
Reputation Points: 10
Solved Threads: 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
}

chipojak
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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

And the answer to your second question is no.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

pandalov101
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 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!!!

abhijeetdani
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 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.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You