Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String equality

 
kevin schmidt
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When comparing two strings, how do you say String 1 is NOT equal to String 2? And also, when using an if statement, what type of OR do you use if you have String 1 equals String 2 OR String 2 equals String 3? TIA.
Kevin
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method String.equals(String) returns a boolean. So, if you have String1 and String2, it returns true if String1 equals String2, false if not.
So, to do String1 not equal to String2, you could do:
String1.equals(String2) == false
For the OR:
if ((String1.equals(String2)) | | (String2.equals(String3)))
[This message has been edited by Matt Senecal (edited September 14, 2001).]
 
kevin schmidt
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I do this:
if ((String1.equals(String2) == false) | | (String2.equals(String3) == false)) {
...
}
 
parul patidar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When comparing two strings, how do you say String 1 is NOT equal to String 2?
if(!string1.equals(string2))
do this;
what type of OR do you use if you have String 1 equals String 2 OR String 2 equals String 3
if(string1.equals(string2)| | string2.equals(string3))
do this;
 
parul patidar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kevin schmidt:
Can I do this:
if ((String1.equals(String2) == false) | | (String2.equals(String3) == false)) {
...
}

u want to check if string1 is equal to string2 or string2 is equal to string3 so above code will not work as it checks
if string1 is not equal to string2 or string2 is not equal to string3 seemy previous post
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic