Comparing strings by their alphabetical order

前端 未结 7 2174
灰色年华
灰色年华 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: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
    } 
    

提交回复
热议问题