I just need a function that will, for two given strings, return negative, positive or zero value. In C, strcmp
is used:
char* a = "Hello";
char* b = "Aargh";
strcmp(a, b); //-1
strcmp(a, a); //0
strcmp(b, a); //1
Does Java have any easy intuitive way to do it, or do I have to use the Comparator
interface?
Does Java have any easy intuitive way to do it?
Yes, it does: java.lang.String
implements Comparable<String>
interface, with compareTo
function:
int comparisonResult = a.compareTo(b);
There is also a case-insensitive version:
int comparisonResult = a.compareToIgnoreCase(b);
The String.compareTo method is the way to go in Java.
How to use it :
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str1 = "tutorials", str2 = "point";
// comparing str1 and str2
int retval = str1.compareTo(str2);
// prints the return value of the comparison
if (retval < 0) {
System.out.println("str1 is less than str2");
}
else if (retval == 0) {
System.out.println("str1 is equal to str2");
}
else {
System.out.println("str1 is greater than str2");
}
}
}
Output :
str1 is less than str2
Example taken from : http://www.tutorialspoint.com/java/lang/string_compareto.htm
那compareTo
呢?
int value = a.compareTo(b);
You can use compareTo but should include a null check as well if you want to be safe:
if (a != null){
result = a.compareTo(b);
} else {
//decide what to do here
}
As it is previously answered, Java has a built-in String.compareTo(String anotherString)
method, you should checkout the runtime sources (you should search google for rt.jar ).
However, here is a working sample with same logic applied on the compareTo
method;
public static int stringCompare(String str1, String str2) {
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
int lim = Math.min(charArray1.length, charArray2.length);
int k = 0;
while (k < lim) {
if (charArray1[k] != charArray2[k]) {
return charArray1[k] - charArray2[k];
}
k++;
}
return charArray1.length - charArray2.length;
}
public class StringCmp1 {
public static void main(String[] args) {
String str1 = "aa";
String str2 = "ad";
System.out.println("Comparison of '" + str1 + "' and '" + str2 + "'");
System.out.println("Custom Method : " + str1.compareTo(str2));
System.out.println("Built-in Method : " + stringCompare(str2, str1));
System.out.println("\nComparison of '" + str2 + "' and '" + str1 + "'");
System.out.println("Custom Method : " + str2.compareTo(str1));
System.out.println("Built-in Method : " + stringCompare(str1, str2));
}
public static int stringCompare(String str1, String str2) {
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
int lim = Math.min(charArray1.length, charArray2.length);
int k = 0;
while (k < lim) {
if (charArray1[k] != charArray2[k]) {
return charArray1[k] - charArray2[k];
}
k++;
}
return charArray1.length - charArray2.length;
}
}
Comparison of 'aa' and 'ad'
Custom Method : -3
Built-in Method : 3
Comparison of 'ad' and 'aa'
Custom Method : 3
Built-in Method : -3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.