Java String Normalize normalize(final String s)

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

Description

trims the string, removes accents, replaces spaces by underscores et remove all non-ascii characters.

License

Open Source License

Parameter

Parameter Description
s string to modify

Return

modified string

Declaration

public static String normalize(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}+");

    /**//from   www  .j  a  v a2s.  c o m
     * 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;
        }
    }

    /**
     * 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("");
    }
}

Related

  1. normaliseConll(String input)
  2. normaliseUnicode(String unicodeText, char[] mappings)
  3. normalize(final String input)
  4. normalize(final String s)
  5. normalize(final String string)
  6. normalize(final String string)
  7. normalize(Object o, StringBuffer sb)