1,105,406 Community Members

String.equals()

Member Avatar
eddy556
Light Poster
32 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 :)

Member Avatar
Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: -1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-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.

Member Avatar
striker3344
Newbie Poster
19 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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..

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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

Member Avatar
Chad W
Newbie Poster
5 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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! :):)

Member Avatar
eddy556
Light Poster
32 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Haha good to see an old post is still helping people

Member Avatar
truebot
Newbie Poster
11 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

am php programmer .. never try java.

thx .

Member Avatar
eddy556
Light Poster
32 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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...

Member Avatar
truebot
Newbie Poster
11 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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:

Member Avatar
Chad W
Newbie Poster
5 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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. :(:(

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
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.

Member Avatar
Chad W
Newbie Poster
5 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
chipojak
Newbie Poster
1 post since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
}

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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

And the answer to your second question is no.

Member Avatar
pandalov101
Newbie Poster
2 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
abhijeetdani
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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!!!

Member Avatar
stultuske
Posting Expert
5,329 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article