Monday, May 27, 2019

Replacing non-ascii characters with their ascii equivalent or removing them

Let say you have this:

var name = "ÄBÖ";

and you want to convert it to

name = "ABO";

The following snippet will do it in Java. IT also will remove the characters that don't have any alternative in ASCII characters such as ß.

String subjectString = "öäü";
subjectString = Normalizer.normalize(subjectString, Normalizer.Form.NFD);
String resultString = subjectString.replaceAll("[^\\x00-\\x7F]", "");