String s1 = "Project";
String s2 = "Sunject";
我想按字母顺序比较上面两个字符串(在本例中为“Project”,然后是“Sunject”,“P”在“S”之前)。 有谁知道如何用Java做到这一点?
答案 0 :(得分:101)
String.compareTo
可能是也可能不是你需要的。
如果您需要字符串的本地化排序,请查看this链接。
答案 1 :(得分:87)
查看String.compareTo
方法。
s1.compareTo(s2)
来自javadocs:
结果是负整数if 这个String对象按字典顺序排列 在参数字符串之前。该 如果是这样,结果是正整数 字符串对象按字典顺序排列 跟随参数字符串。该 如果字符串是,则结果为零 等于; compareTo完全返回0 当equals(Object)方法会 返回true。
答案 2 :(得分:30)
String a = "...";
String b = "...";
int compare = a.compareTo(b);
if (compare < 0) {
//a is smaller
}
else if (compare > 0) {
//a is larger
}
else {
//a is equal to b
}
答案 3 :(得分:6)
您可以调用string的compareTo方法(java.lang.String.compareTo)。此功能已有详细记录on the java documentation site。
这是一个演示它的简短程序:
class StringCompareExample {
public static void main(String args[]){
String s1 = "Project"; String s2 = "Sunject";
verboseCompare(s1, s2);
verboseCompare(s2, s1);
verboseCompare(s1, s1);
}
public static void verboseCompare(String s1, String s2){
System.out.println("Comparing \"" + s1 + "\" to \"" + s2 + "\"...");
int comparisonResult = s1.compareTo(s2);
System.out.println("The result of the comparison was " + comparisonResult);
System.out.print("This means that \"" + s1 + "\" ");
if(comparisonResult < 0){
System.out.println("lexicographically precedes \"" + s2 + "\".");
}else if(comparisonResult > 0){
System.out.println("lexicographically follows \"" + s2 + "\".");
}else{
System.out.println("equals \"" + s2 + "\".");
}
System.out.println();
}
}
这是一个现场演示,显示它有效:http://ideone.com/Drikp3
答案 4 :(得分:4)
对于国有化后的字母顺序,请使用Collator
。
//Get the Collator for US English and set its strength to PRIMARY
Collator usCollator = Collator.getInstance(Locale.US);
usCollator.setStrength(Collator.PRIMARY);
if( usCollator.compare("abc", "ABC") == 0 ) {
System.out.println("Strings are equivalent");
}
有关支持的区域设置的列表,请参阅JDK 8 and JRE 8 Supported Locales。
答案 5 :(得分:1)
import java.io.*;
import java.util.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int n =Integer.parseInt(sc.nextLine());
String arr[] = new String[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextLine();
}
for(int i = 0; i <arr.length; ++i) {
for (int j = i + 1; j <arr.length; ++j) {
if (arr[i].compareTo(arr[j]) > 0) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i = 0; i <arr.length; i++) {
System.out.println(arr[i]);
}
}
}
答案 6 :(得分:1)
String s1 = "Project";
String s2 = "Sunject";
//print smaller one using compareTo() function
if(s1.compareTo(s2)<0) System.out.println(s1);
//if s1 is smaller then function returns negative which is less than 0 so s1
//is smaller
else System.out.println(s2); // else s2 is smaller
//print larger one using compareTo() function
if(s1.compareTo(s2)>0) System.out.println(s1);
//is s1 is larger function will give positive so print s1 else s2
else System.out.println(s2);
答案 7 :(得分:0)
按照其他人的建议,您可以使用String.compareTo(String)
。
但是,如果要对字符串列表进行排序,并且需要Comparator
,则不必实现它,可以使用Comparator.naturalOrder()
或Comparator.reverseOrder()
。