receives a string as input parameter and normalizes it to lowercase, removes leading and trailing spaces and strips accents from the input string. - Java java.lang

Java examples for java.lang:String Strip

Description

receives a string as input parameter and normalizes it to lowercase, removes leading and trailing spaces and strips accents from the input string.

Demo Code


//package com.java2s;
import java.text.Normalizer;

public class Main {
    public static void main(String[] argv) throws Exception {
        String item = "java2s.com";
        System.out.println(normalizeString(item));
    }/*  ww  w.  j  a v a2  s  . com*/

    /**
     * This method receives a string as input parameter and normalizes it to 
     * lowercase, removes leading and trailing spaces and strips accents from 
     * the input string. 
     * 
     * @param item string to be normalized
     * 
     * @return normalized string 
     */
    public static String normalizeString(String item) {

        // remove invalid characters
        String result = removeInvalidCharacters(item);

        // remove leading and trailing spaces, normalize to lowercase, decompose string 
        result = Normalizer.normalize(result.trim().toLowerCase(),
                Normalizer.Form.NFKD);

        //remove the accent characters from the decomposed string using a regular expression
        result = result.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");

        //remove posible middle white spaces
        result = result.replaceAll("([\\s]+)", " ");

        return result;
    }

    /**
     * This methods removes from string: \n \t ; |
     * @param sentence
     * @return string without invalid characters
     */
    public static String removeInvalidCharacters(String sentence) {
        String result = sentence.replaceAll("\\n|\\t|\"|;|\\|", "");
        return result.replaceAll("&nbsp", " ");
    }
}

Related Tutorials