How can I compare two strings in java and define which of them is smaller than the other alphabetically?

前端 未结 3 1253
余生分开走
余生分开走 2020-11-30 02:27

I want to use the binary search algorithm to search the string which has been entered by the user in a very big sorted file. I can not compare the string which has been ente

3条回答
  •  执笔经年
    2020-11-30 02:48

    If you would like to ignore case you could use the following:

    String s = "yip";
    String best = "yodel";
    int compare = s.compareToIgnoreCase(best);
    if(compare < 0){
        //-1, --> s is less than best. ( s comes alphabetically first)
    }
    else if(compare > 0 ){
    // best comes alphabetically first.
    }
    else{
        // strings are equal.
    }
    

提交回复
热议问题