Java String Accent removeAccents(final String s)

Here you can find the source of removeAccents(final String s)

Description

replace accentuated characters by their non-accentuated equivalents.

License

Open Source License

Parameter

Parameter Description
s a parameter

Declaration

public static String removeAccents(final String s) 

Method Source Code

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

import java.text.Normalizer;

import java.util.regex.Pattern;

public class Main {
    private static final Pattern DIACRITICALMARKS_PATTERN = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");

    /**/* www. j a  v  a  2  s .c o m*/
     * replace accentuated characters by their non-accentuated equivalents.
     *
     * @param s
     * @return
     */
    public static String removeAccents(final String s) {
        String nfdNormalizedString = Normalizer.normalize(s, Normalizer.Form.NFD);
        return DIACRITICALMARKS_PATTERN.matcher(nfdNormalizedString).replaceAll("");
    }

    /**
     * trims the string, removes accents, replaces spaces by underscores et remove all non-ascii characters. Also adds an underscore at the beginning if the
     * string starts with a number.
     *
     * This method may be useful when transforming a random string into a variable name or file name, for example.
     *
     * @param s
     *            string to modify
     * @return modified string
     */
    public static String normalize(final String s) {
        String tmp = removeAccents(s).replaceAll("[^a-zA-Z0-9_]", "").trim().replaceAll("\\p{Space}", "_");
        if (s.matches("^[0-9].*")) {
            return "_" + tmp;
        } else {
            return tmp;
        }
    }
}

Related

  1. getDeAccentLoweredChars(String word)
  2. normalizeByRemovingAccent(final String string)
  3. removeAccent(String s)
  4. removeAccent(String s)
  5. removeAccent(String strIn)
  6. removeAccents(final String value)
  7. removeAccents(final String value)
  8. removeAccents(String input)
  9. removeAccents(String s)