[英]Get integer difference between string just like strcmp
I just need a function that will, for two given strings, return negative, positive or zero value. 我只需要一个函数,对于两个给定的字符串,将返回负值,正值或零值。 In C,
strcmp
is used: 在C中,使用
strcmp
:
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? Java是否有任何简单直观的方法来执行此操作,还是必须使用
Comparator
接口?
Does Java have any easy intuitive way to do it?
Java是否有任何简单直观的方法来做到这一点?
Yes, it does: java.lang.String
implements Comparable<String>
interface, with compareTo
function: 是的,它可以:
java.lang.String
使用compareTo
函数实现Comparable<String>
接口:
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. String.compareTo方法是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 示例取自: 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: 您可以使用compareTo,但是如果您想安全的话,还应该包括一个空检查:
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 ). 正如先前的回答,Java具有内置的
String.compareTo(String anotherString)
方法,您应该签出运行时源(应在google中搜索rt.jar )。
However, here is a working sample with same logic applied on the compareTo
method; 但是,这是一个工作示例,对
compareTo
方法应用了相同的逻辑。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.