50

Possible Duplicates:
Remove diacritical marks (ń ǹ ň ñ ṅ ņ ṇ ṋ ṉ ̈ ɲ ƞ ᶇ ɳ ȵ) from Unicode chars
Is there a way to get rid of accents and convert a whole string to regular letters?

How can i do this? Thanks for the help

Community
  • 1
  • 1
lacas
  • 13,928
  • 30
  • 109
  • 183

3 Answers3

152

I think your question is the same as these:

and hence the answer is also the same:

String convertedString = 
       Normalizer
           .normalize(input, Normalizer.Form.NFD)
           .replaceAll("[^\\p{ASCII}]", "");

See

Example Code:

final String input = "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ";
System.out.println(
    Normalizer
        .normalize(input, Normalizer.Form.NFD)
        .replaceAll("[^\\p{ASCII}]", "")
);

Output:

This is a funky String

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
12

You can use java.text.Normalizer to separate base letters and diacritics, then remove the latter via a regexp:

public static String stripDiacriticas(String s) {
    return Normalizer.normalize(s, Form.NFD)
        .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 1
    I used something similar that did the job: Pattern.compile("\\p{InCombiningDiacriticalMarks}+").matcher(nfdNormalizedString).replaceAll(""); – Adriano Mar 04 '13 at 15:15
10

First - you shouldn't. These symbols carry special phonetic properties which should not be ignored.

The way to convert them is to create a Map that holds each pair:

Map<Character, Character> map = new HashMap<Character, Character>();
map.put('á', 'a');
map.put('é', 'e');
//etc..

and then loop the chars in the string, creating a new string by calling map.get(currentChar)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140