Java String Accent compareIgnoreCaseAndAccent(String string1, String string2, Locale locale)

Here you can find the source of compareIgnoreCaseAndAccent(String string1, String string2, Locale locale)

Description

Performs a string comparison ignoring case and accent.

License

Open Source License

Parameter

Parameter Description
string1 string to be compared.
string2 string to be compared.
locale The locale to perform the comparison.

Return

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Declaration

public static int compareIgnoreCaseAndAccent(String string1, String string2, Locale locale) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.Collator;

import java.util.Locale;

public class Main {
    /**/*  w  w  w.ja  va 2  s.c o  m*/
     * Performs a string comparison ignoring case and accent. Beginning and ending spaces are trimmed.
     * 
     * @param string1
     *          string to be compared.
     * @param string2
     *          string to be compared.
     * @param locale
     *          The locale to perform the comparison.
     * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the
     *         second.
     */
    public static int compareIgnoreCaseAndAccent(String string1, String string2, Locale locale) {
        if (string1 == null || string2 == null) {
            if (string1 == null && string2 == null)
                return 0;
            if (string1 == null)
                return -1;
            return 1;
        }

        Collator collator = Collator.getInstance(locale);
        collator.setStrength(Collator.PRIMARY);
        collator.setDecomposition(Collator.FULL_DECOMPOSITION);

        return collator.compare(string1, string2);
    }

    /**
     * Performs a string comparison ignoring case and accent. Beginning and ending spaces are trimmed.
     * 
     * @param string1
     *          string to be compared.
     * @param string2
     *          string to be compared.
     * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the
     *         second.
     */
    public static int compareIgnoreCaseAndAccent(String string1, String string2) {
        return compareIgnoreCaseAndAccent(string1, string2, Locale.getDefault());
    }
}

Related

  1. deAccent(final @Nonnull String string)
  2. deAccent(String str)
  3. deAccent(String str)
  4. deAccent(String str)