java中的g_ascii_strcasecmp等价物

时间:2012-05-16 22:51:07

标签: java c++ c glib

我有一个使用g_ascii_strcasecmp函数排序的单词列表。我需要在java中处理这个列表。 java中的等价排序函数是什么?为了实现二进制搜索,我需要一个正确的比较函数。到目前为止,我有下面的功能,但它并不总能产生正确的结果。

public int compareStrings(String str) {
    Collator collator = Collator.getInstance();//TODO: implement locale?
    return collator.compare(this.wordString, str);
}

更新。列表示例:“T,t,T'ai Chi Ch'uan,t'other,T-,T-bone,T-bone steak,T-junction,tabasco,Tabassaran,tabby”。

2 个答案:

答案 0 :(得分:1)

我不会在阅读其Javadoc时使用Collator,因为您无法控制字符串的比较方式。您可以选择语言环境,但该语言环境如何告诉Collator如何比较字符串是不可能的。

如果您知道字符串中的字符都是ASCII字符,那么我只使用String.compareTo()方法,该方法根据unicode字符值按字典顺​​序排序。如果字符串中的所有字符都是ASCII字符,则它们的unicode字符值将是它们的ASCII值,因此按字典顺序排序其unicode值将与按字典顺序排序的ASCII值相同,这似乎是g_ascii_stcasecmp确实。如果您需要不区分大小写,可以使用String.compareToIgnoreCase()


正如我在评论中所指出的,我认为你需要编写自己的比较函数。您需要遍历字符串中的字符,跳过不在ASCII范围内的字符。所以像这样的东西,这是一个简单的,愚蠢的实现,需要加强,以涵盖我想象g_ascii_strcasecmp所做的极端情况:

public int compareStrings(String str) {
    List<Character> myAsciiChars = onlyAsciiChars(this.wordString);
    List<Character> theirAsciiChars = onlyAsciiChars(str);

    if (myAsciiChars.size() > theirAsciiChars.size()) {
        return 1;
    }
    else if (myAsciiChars.size() < theirAsciiChars.size()) {
        return -1;
    }

    for (int i=0; i < myAsciiChars.size(); i++) {
        if (myAsciiChars.get(i) > theirAsciiChars.get(i)) {
            return 1;
        }
        else if (myAsciiChars.get(i) < theirAsciiChars.get(i)) {
            return -1;
        }
    }

    return 0;
}

private final static char MAX_ASCII_VALUE = 127; // (Or 255 if using extended ASCII)

private List<Character> onlyAsciiChars(String s) {
    List<Character> asciiChars = new ArrayList<>();
    for (char c : s.toCharArray()) {
        if (c <= MAX_ASCII_VALUE) {
            asciiChars.add(c);
        }
    }
    return asciiChars;
}

答案 1 :(得分:0)

我决定分享我提出的方法:

    /**
     * Compares two strings, ignoring the case of ASCII characters. It treats
     * non-ASCII characters taking in account case differences. This is an 
     * attempt to mimic glib's string utility function 
     * <a href="http://developer.gnome.org/glib/2.28/glib-String-Utility-Functions.html#g-ascii-strcasecmp">g_ascii_strcasecmp ()</a>.
     *
     * This is a slightly modified version of java.lang.String.CASE_INSENSITIVE_ORDER.compare(String s1, String s2) method.
     * 
     * @param str1  string to compare with str2
     * @param str2  string to compare with str1
     * @return      0 if the strings match, a negative value if str1 < str2, or a positive value if str1 > str2
     */
    private static int compareToIgnoreCaseASCIIOnly(String str1, String str2) {
        int n1 = str1.length();
        int n2 = str2.length();
        int min = Math.min(n1, n2);
        for (int i = 0; i < min; i++) {
            char c1 = str1.charAt(i);
            char c2 = str2.charAt(i);
            if (c1 != c2) {
                if ((int) c1 > 127 || (int) c2 > 127) { //if non-ASCII char
                    return c1 - c2;
                } else {
                    c1 = Character.toUpperCase(c1);
                    c2 = Character.toUpperCase(c2);
                    if(c1 != c2) {
                        c1 = Character.toLowerCase(c1);
                        c2 = Character.toLowerCase(c2);
                        if(c1 != c2) {
                            return c1 - c2;
                        }
                    }
                }
            }
        }
        return n1 - n2;
    }
相关问题