1

In PHP, I can do:

$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);

means that in a string that contains any char like à, á, a, ả, ......will be replace by a.

How can I do the equivalence in Java?

ipkiss
  • 13,311
  • 33
  • 88
  • 123
  • 3
    Please see: http://stackoverflow.com/questions/3322152/java-getting-rid-of-accents-and-converting-them-to-regular-letters – J.K. Jan 27 '13 at 14:11

3 Answers3

10

You might want to use a much more generic solution for this problem:

import java.text.Normalizer;
import java.text.Normalizer.Form;

// ...

public static String removeAccents(String text) {
    return text == null ? null
        : Normalizer.normalize(text, Form.NFD)
            .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}

This removes all diacritical marks, from any letter, in any script.

  • 1
    When I try to run this code my compiler says change encoding or remove the characters which are not supported. How to make it work? – Achintya Jha Jan 27 '13 at 14:39
  • It means that your source file contains characters the compiler doesn't understand. I can't tell what's going on without seeing the whole source file. –  Jan 27 '13 at 19:39
3

Something very similar:

String output = input.replaceAll("[àáạảãâầấậẩẫăằắặẳẵ]","a");
assylias
  • 321,522
  • 82
  • 660
  • 783
1
str.replace("Your character sequence" , "a");
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39