java字符串比较的原理

    public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
       //比较字符数组中的字符,不等则返回两者的ascii差值
if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }

 

posted @ 2021-02-06 22:07  轻风青枫  阅读(145)  评论(0编辑  收藏  举报