Comparing strings by their alphabetical order

前端 未结 7 2083
灰色年华
灰色年华 2020-11-27 04:26
String s1 = \"Project\";
String s2 = \"Sunject\";

I want to compare the two above string by their alphabetic order (which in this case \"Project\"

相关标签:
7条回答
  • 2020-11-27 04:39

    You can call either string's compareTo method (java.lang.String.compareTo). This feature is well documented on the java documentation site.

    Here is a short program that demonstrates it:

    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();
        }
    }
    

    Here is a live demonstration that shows it works: http://ideone.com/Drikp3

    0 讨论(0)
  • 2020-11-27 04:39

    For alphabetical order following nationalization, use 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");
    }
    

    For a list of supported locales, see JDK 8 and JRE 8 Supported Locales.

    0 讨论(0)
  • 2020-11-27 04:47
    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]);
            }
       }
    }
    
    0 讨论(0)
  • 2020-11-27 04:56

    String.compareTo might or might not be what you need.

    Take a look at this link if you need localized ordering of strings.

    0 讨论(0)
  • 2020-11-27 04:56
    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
    } 
    
    0 讨论(0)
  • 2020-11-27 04:57

    Take a look at the String.compareTo method.

    s1.compareTo(s2)
    

    From the javadocs:

    The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

    0 讨论(0)
提交回复
热议问题