String case Ignore Compare - Android java.lang

Android examples for java.lang:String Case

Description

String case Ignore Compare

Demo Code


public class Main{
    public static int caseIgnoreCompare(String s1, String s2) {
        if (s1 == s2) {
            return 0;
        }//  ww  w  .  j  a  va  2 s .  co m
        return toLowerString(s1).compareTo(
                toLowerString(s2));
    }
    public static String toLowerString(String s) {
        int idx = 0;
        for (; idx < s.length(); idx++) {
            char c = s.charAt(idx);
            if (c >= 'A' && c <= 'Z') {
                break;
            }
        }
        if (idx == s.length()) {
            return s;
        }
        StringBuilder buf = new StringBuilder(s.substring(0, idx));
        for (; idx < s.length(); idx++) {
            buf.append(toLower(s.charAt(idx)));
        }
        return buf.toString();
    }
    public static char toLower(char c) {
        if (c >= 'A' && c <= 'Z') {
            c += 0x20;
        }
        return c;
    }
}

Related Tutorials