Normalize string except -

I have the following code example:

String n = "Péña";
n = Normalizer.normalize(n, Normalizer.Form.NFC);

How to normalize a string nexcept ñ?

And not only this line, I make the form, and I want to save only ñ'severything else without diacritics.

+2
source share
1 answer

Replace all occurrences "-" with the non-printable character "\ 001", so "Péña" will become "Pé \ 001a". Then call Normalizer.normalize()to expand "é" into "e" and a separate diacritical mark. Finally, remove the diacritics and convert the unprintable character back to "-".

String partiallyNormalize(String string)
{
    string = string.replace('ñ', '\001');
    string = Normalizer.normalize(string, Normalizer.Form.NFD);
    string = string.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
    string = string.replace('\001', 'ñ');
    return string;
}

UTF-8 ?, , .

+2

Source: https://habr.com/ru/post/1689827/


All Articles