Comparing to string with each other in Java
I'm looking for a solution to compare two strings with each other. I have already found a few suggestions but i'm not really understanding how to do it. I want to do something like that:
String a = 23;
String b = 2;
if (a < b)
System.out.print开发者_StackOverflowln("...");
else ..
I've found the compareTo method but i don't get the idea behind it. The code would be something like:
String a = 23;
String b = 2;
if (a.compareTo(b) < 0)
System.out.println("...");
else ..
But why should i compare to strings with each other and then compare it to zero?? I'm really confused.
You cannot do a < b
in java with String
. You have to use compareTo or Comparator.
You cannot write String a = 23;
also. You can convert them to integer and then do a < b
.
Read the API for compareTo
a.compareTo(b)
The result of this method call:
is a negative number (hence < 0) if a precedes b alphabetically (e.g. a="apple", b="bananna")
is 0 if it is the same string
and is a positive number if a is after b alphabetically
If you want to compare numeric values, either do your comparison on Integers or first parse the strings e.g.
if (Integer.parseInt(a) < Integer.parseInt(b)){
...
} else {
...
}
Comparing a
and b
directly just compares their internal references.
Note (as Jon pointed out) that you can't even use the <
operators on Strings, although you can use the ==
operator.
However to compare their contents use you must use the equals()
or comparesTo()
methods.
To demostrate the ==
issues:
public class foobar {
static public void main(String[] args) {
// two identical strings
String a = "foo";
String b = "foo";
// and the result of comparing them
System.out.println(" == returns " + (a == b));
System.out.println(" String.equals() returns " + a.equals(b));
// append the same string on the end of each
a += "bar";
b += "bar";
// and compare them again
System.out.println(" == returns " + (a == b));
System.out.println(" String.equals() returns " + a.equals(b));
}
}
% java foobar
== returns true
String.equals() returns true
== returns false
String.equals() returns true
When a
and b
are both initialised from the same string then string interning
initialises both references to look at that immutable string. This means that initially a == b
is true.
However as soon as you modify them, even if the contents end up the same, the ==
test fails.
Do like this, it will give the correct result
String s = "22";
string s1 = "2";
string output = "";
if (s.CompareTo(s1) > 0)
output = "S is grater";
else
output = "s is smaller";
If you need compare numbers you need override compare() method, because java not allow to override operators. You can use something like this:
public class TestString implements Comparator<String> {
private static TestString ts = new TestString();
static public int compareStrings(String s1, String s2) {
return ts.compare(s1, s2);
}
public int compare(String s1, String s2) {
Integer i1 = Integer.parseInt(s1);
Integer i2 = Integer.parseInt(s2);
if (i1 == i2) {
return 0;
} else if (i1 < i2) {
return -1;
} else {
return 1;
}
}
static public void main(String[] args) {
String s1 = "10";
String s2 = "3";
if (TestString.compareStrings(s1, s2) < 0) {
System.out.println("<");
} else if (TestString.compareStrings(s1, s2) > 0) {
System.out.println(">");
}
}
}
But if you need actually compare values (not for ordering) you look at program design, maybe there need some changes.
Don't use String if you want to use the larger than, smaller than operators. Use int then if you want to change it to string later useInteger.toString(a)
精彩评论