Home arrow static arrow Java Programming [Archive] - Question about comparing two Strings
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Question about comparing two Strings
This topic has 24 replies on 2 pages.    1 | 2 | Next »

Posts:536
Registered: 6/29/03
Question about comparing two Strings  
Jun 30, 2004 12:46 AM



 
Hello, everyone!

I have a question about comparing two Strings in Java. Current compare function of comparing two Strings does not apply to null Strings, i.e. when using comparing function of class String, we must sure that neither of the two Strings comparing to each other are null, or else, NullPointerException will be thrown.

I want to know whether Java has some direct String comparing mechanisms which can apply to null Strings? Such are the requirements I want it to be,

1. Null String is always equal to null String;
2. Null Stirng is never equal to a non-null String;
3. If two non-null Strings contain the same sequence of characters, they are the equal.

Surely I can implement this utility easily, and I just want to know whether Java provides this kind of function directly.

Thanks in advance,
George

 

Posts:951
Registered: 6/7/01
Re: Question about comparing two Strings  
Jun 30, 2004 12:58 AM (reply 1 of 24)



 
How about :
String s1 = "String1"String s2 = "String2"boolean b = s1 != null && s2 != null && s1.equals(s2);
 

Posts:1,995
Registered: 12/10/00
Re: Question about comparing two Strings  
Jun 30, 2004 1:06 AM (reply 2 of 24)



 
Reading your post I assume that you are referring to the compareTo(...) functions.
If you want to check only equality then you'd better use equals(Object) instead. It checks if the orher Object is a String instance thus handling trhe case when it is null.

The check would be like
    boolean equal = (null==s1)?(null==s2):s1.equals(s2);


Mike
 

Posts:536
Registered: 6/29/03
Re: Question about comparing two Strings  
Jun 30, 2004 1:08 AM (reply 3 of 24)



 
If s1 and s2 are both null Stirngs, I want to get true and your statement will rsult in false.

BTW: my question is whether Java has internal mechanism that can apply to null String comparing and not how to implemente this function directly.

George

 

Posts:111
Registered: 12/21/97
Re: Question about comparing two Strings  
Jun 30, 2004 1:09 AM (reply 4 of 24)



 
I do not know of any direct mechanism in java to test 2 null strings, but most of the time, you would only need to test for null on one of the strings. Instead of writing
s.equals("hello world");

change it to:
"hello world".equals(s);

which would handle the null condition nicely when s == null. With that in mind, you would only have to test for null for one string only:
public boolean equals(String s1, String s2) {    if (s1 == null || !s1.equals(s2))        return false;    return true;}
 

Posts:536
Registered: 6/29/03
Re: Question about comparing two Strings  
Jun 30, 2004 1:13 AM (reply 5 of 24)



 
Thanks, Mike buddy!

Your sample code conforms to my comparing logic exactly. Does Java has any comparing functions that conform to the same logic directly? So, we can omit the "null" checking in your sample code.

Best regards,
George

 

Posts:536
Registered: 6/29/03
Re: Question about comparing two Strings  
Jun 30, 2004 1:20 AM (reply 6 of 24)



 
Thanks, aghost buddy!

I have tested your sample, I found that when two Strings are both null, it returns false. But I want it to be true, i.e. a null String is always equal to a null String. So I think I need to check wheher s1 or s2 is null before invoke equal function. So, comparing two times with null and not just one time.

Best regards,
George

 

Posts:111
Registered: 12/21/97
Re: Question about comparing two Strings  
Jun 30, 2004 1:29 AM (reply 7 of 24)



 
oops, sorry.... then mike's solution would be the nearest thing to what you want.
 

Posts:147
Registered: 26.01.04
Re: Question about comparing two Strings  
Jun 30, 2004 1:52 AM (reply 8 of 24)



 
hmm im not sure but i think the following would work as well:
if(s1 == s2 || s1.equals(s2))   return  truereturn false;


or alternatly, if there are very seldom null objects:
try {   if(s1.equals(s2))       return true;} catch(NullpointerException e) {    if(s2 == null) //or s1 == s2 if that returns true for null == null        return true;} return false;


i will test it later at home.
 

Posts:111
Registered: 12/21/97
Re: Question about comparing two Strings  
Jun 30, 2004 1:58 AM (reply 9 of 24)



 
TheosTheon's first proposed solution cannot work 'cos if s1 is a null, then s1.equals(s2) would throw a NullPointerException.
 

Posts:208
Registered: 4/23/03
Re: Question about comparing two Strings  
Jun 30, 2004 2:05 AM (reply 10 of 24)



 
[code]
if(s1==s2) {
return true;
}
else if(s1==null || s2==null) {
return false;
}
else if(s1.equals(s2)) {
return true;
}
return false;
 

Posts:208
Registered: 4/23/03
Re: Question about comparing two Strings  
Jun 30, 2004 2:05 AM (reply 11 of 24)



 
sorry the last line should be
 return false
 

Posts:1,923
Registered: 4/17/03
Re: Question about comparing two Strings  
Jun 30, 2004 2:05 AM (reply 12 of 24)



 
Your first case could result in an NPE.

boolean equal = (s1 == s2 || s1 != null && s1.equals(s2));

The second case is kind of ugly.

If you are comparing two strings for equality, I would probably think if either is null then it would be more common to throw an IllegalArgumentException. (Do databases return null or the empty string?)
But in a compator, you would probably want to move all the null entries in an array to the end.
 

Posts:147
Registered: 26.01.04
Re: Question about comparing two Strings  
Jun 30, 2004 2:10 AM (reply 13 of 24)



 
TheosTheon's first proposed solution cannot work 'cos
if s1 is a null, then s1.equals(s2) would throw a
NullPointerException.

ahh yes, forgot the case if theyre not equal :x
 

Posts:6
Registered: 10/27/03
Re: Question about comparing two Strings  
Jun 30, 2004 3:43 AM (reply 14 of 24)



 
Hi,

1. Null String is always equal to null String;
2. Null Stirng is never equal to a non-null String;
3. If two non-null Strings contain the same sequence of characters, they are the equal.

there is no direct comparing mechanism in Java for your needs. The problem is that Java is designed as object oriented but also has simple data types like "int", "boolean" and, if you want so, "null".
You can compare in a way by using "==", but that operator is not object oriented and therefore will not work with String the way you expect it. Writing

string1==string2
would give:

true
if both are null
false
if one is null and the other isn't, but
true
only if the two pointers are identical (point to the same place in memory) and not if the characters in your string are the same.

To compare a string to another String(-Object) you have to be shure that at least the first one is not null, because you have to call a method of "String" - String.equals(anotherString). That only works if the first String is an object, e.g. not null. The only way to do so is:

boolean equal=false;if (string1!=null) equal=string1.equals(string2);
 
This topic has 24 replies on 2 pages.    1 | 2 | Next »