StringUtils.java解析2

/**
    *    public boolean equals(Object anObject) {
    *    if (this == anObject) {
    *        return true;
    *   }
    *    if (anObject instanceof String) {
    *        String anotherString = (String) anObject;
    *        int n = value.length;
    *        if (n == anotherString.value.length) {
    *            char v1[] = value;
    *            char v2[] = anotherString.value;
    *            int i = 0;
    *            while (n-- != 0) {
    *                if (v1[i] != v2[i])
    *                        return false;
    *               i++;
    *            }
    *            return true;
    *        }
    *    }
    *    return false;
    *}
    *
    *
    */
    //首先判断str1是否为null若是为null,那就返回str2跟null比较的结果,若是不是null,那就调用str1.equals(str2)
    public static boolean equals(String str1, String str2)
    {
        return str1 != null ? str1.equals(str2) : str2 == null;
    }
    //忽略字符串中大小写,比较字符串是否相等
    public static boolean equalsIgnoreCase(String str1, String str2)
    {
        return str1 != null ? str1.equalsIgnoreCase(str2) : str2 == null;
    }
    
    /**
    *下面是String.java中indexOf方法的源码
    *这里涉及到java低代理和高代理的知识,请自行百度。
    *    public int indexOf(int ch, int fromIndex) {
    *    final int max = value.length;
    *    if (fromIndex < 0) {
    *        fromIndex = 0;
    *   } else if (fromIndex >= max) {
    *        // Note: fromIndex might be near -1>>>1.
    *        return -1;
    *    }
    *    if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
    *        // handle most cases here (ch is a BMP code point or a
    *        // negative value (invalid code point))
    *        final char[] value = this.value;
    *        for (int i = fromIndex; i < max; i++) {
    *            if (value[i] == ch) {
    *               return i;
    *            }
    *        }
    *        return -1;
    *    } else {
    *        return indexOfSupplementary(ch, fromIndex);
    *    }
    *}
    *
    */
    //这个函数很简单,就是返回指定字符串中的指定字符首次出现的位置,没有则返回-1,从0(即第一个字符开始)
    public static int indexOf(String str, char searchChar)
    {
        if (isEmpty(str))
            return -1;
        else
            return str.indexOf(searchChar);
    }
    //这个函数与上面函数相似,只不过是能够本身设定开始查找指定字符的位置了,即fromIndex也就是下面的startPos
    public static int indexOf(String str, char searchChar, int startPos)
    {
        if (isEmpty(str))
            return -1;
        else
            return str.indexOf(searchChar, startPos);
    }
    //注意这个函数的两个参数都是String类型
    public static int indexOf(String str, String searchStr)
    {
        if (str == null || searchStr == null)
            return -1;
        else
            return str.indexOf(searchStr);
    }java

相关文章
相关标签/搜索