Java 不区分大小写比较字符串

时间:2022-05-13 01:00:41

在项目中需要实现一个功能,获取数据库表的所有列,比如user表有 [name,age,height]三个字段,在插入insert之前需要判断插入的列是否在表的列字段中,例如需要插入的数据为

{ name:"机哥", age:26, Height:"60kg", salary:"100块"} ,插入之前比较,发现salary不在表中,把salary剔除。然后发现Height也被剔除了,因为不在列中。

jdk库没有现成的方法(最接近的是contains方法),自己实现了一个,如下:

public static void main(String[] args) {
        String orignal = "Height";

        String[] arry = {"name","age","height"};

        if(listContains(Arrays.asList(arry), orignal)){
            System.out.printf("success");
        }
    }

    private static boolean listContains(List<String> list, String ele){
        for(String t:list)
        {
            if(t.equalsIgnoreCase(ele))
                return true;
        }
        return false;
    }

原来用的是ArrayList.contains()方法,是区分大小写的,contains内部通过indexOf(ele) > 0实现,indexOf代码如下:

public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i])) //equals比较
                    return i;
        }
        return -1;
    }

然后看下equalsIngoreCase()方法实现

 public boolean equalsIgnoreCase(String anotherString) {
        return (this == anotherString) ? true
                : (anotherString != null)
                && (anotherString.value.length == value.length)
                && regionMatches(true, 0, anotherString, 0, value.length);
    }

首先比较对象引用相等,然后串长度一样,最后调用regionMatches()方法

public boolean regionMatches(boolean ignoreCase, int toffset,
            String other, int ooffset, int len) {
        char ta[] = value;
        int to = toffset;
        char pa[] = other.value;
        int po = ooffset;
        // Note: toffset, ooffset, or len might be near -1>>>1.
        if ((ooffset < 0) || (toffset < 0)
                || (toffset > (long)value.length - len)
                || (ooffset > (long)other.value.length - len)) {
            return false;
        }
        while (len-- > 0) {
            char c1 = ta[to++];
            char c2 = pa[po++];
            if (c1 == c2) {
                continue;
            }
            if (ignoreCase) {
                // If characters don't match but case may be ignored,
                // try converting both characters to uppercase.
                // If the results match, then the comparison scan should
                // continue.
                char u1 = Character.toUpperCase(c1);
                char u2 = Character.toUpperCase(c2);
                if (u1 == u2) {
                    continue;
                }
                // Unfortunately, conversion to uppercase does not work properly
                // for the Georgian alphabet, which has strange rules about case
                // conversion.  So we need to make one last check before
                // exiting.
                if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
                    continue;
                }
            }
            return false;
        }
        return true;
    }

regionMatches这个函数有点意思,首先是边界检查,如果不符合规则返回false;

之后开始字符比较,如果相等,继续比较;如不相等并忽略大小写,都转成大写比较,如果还是不相等,都转成小写比较,最后还是不相等,返回false。

为什么转成大小比较不相等还不够,还要转成小写的,那是因为有格鲁吉亚字母(Georgian alphabet):大写不等还不一定不等,还需要转小写   比如 ქართული დამწერლობა   哈哈  有意思。