92

I want to change this sentence :

Et ça sera sa moitié.

To :

Et ca sera sa moitie.

Is there an easy way to do this in Java, like I would do in Objective-C ?

NSString *str = @"Et ça sera sa moitié.";
NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *newStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
Rob
  • 15,732
  • 22
  • 69
  • 107

5 Answers5

176

Finally, I've solved it by using the Normalizer class.

import java.text.Normalizer;

public static String stripAccents(String s) 
{
    s = Normalizer.normalize(s, Normalizer.Form.NFD);
    s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
    return s;
}
Abdull
  • 26,371
  • 26
  • 130
  • 172
Rob
  • 15,732
  • 22
  • 69
  • 107
  • 3
    In order to correctly transform some strings, I used **`Form.NFKD`** ("Compatibility decomposition.") – Anthony O. Jul 24 '13 at 12:31
  • It seems that Normalizer is deprecated and Normalizer2 should be use instead http://icu-project.org/apiref/icu4j/com/ibm/icu/text/Normalizer2.html – ykatchou Feb 12 '19 at 12:14
  • @ykatchou, what makes you believe `java.text.Normalizer` to be deprecated? – Abdull Mar 13 '19 at 17:49
  • See here : http://icu-project.org/apiref/icu4j/com/ibm/icu/text/Normalizer.html ("This API has been replaced by the Normalizer2 class and is only available for backward compatibility") – ykatchou Sep 03 '19 at 13:33
  • 2
    @ykatchou you refer to "com.ibm.icu.text.Normalizer", but answer is about "java.text.Normalizer" – David S. Sep 30 '19 at 12:20
  • But it changes the sense of text on different languages: stripAccents("йод,ëлка,wäre") //иод,елка,ware. How to remove only acute accents? Or any selected set of diacritics? – KursikS Oct 08 '20 at 11:15
121

Maybe the easiest and safest way is using StringUtils from Apache Commons Lang

StringUtils.stripAccents(String input)

Removes diacritics (~= accents) from a string. The case will not be altered. For instance, 'à' will be replaced by 'a'. Note that ligatures will be left as is.

StringUtils.stripAccents()

Ondrej Bozek
  • 10,987
  • 7
  • 54
  • 70
12

I guess the only difference is that I use a + and not a [] compared to the solution. I think both works, but it's better to have it here as well.

String normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
String accentRemoved = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
7

For kotlin

fun stripAccents(s: String): String 
{
    var string = Normalizer.normalize(s, Normalizer.Form.NFD)
    string = Regex("\\p{InCombiningDiacriticalMarks}+").replace(string, "")
    return  string
}
Tristan Richard
  • 3,385
  • 1
  • 15
  • 17
5

Assuming you are using Java 6 or newer, you might want to take a look at Normalizer, which can decompose accents, then use a regex to strip the combining accents.

Otherwise, you should be able to achieve the same result using ICU4J.

hertzsprung
  • 9,445
  • 4
  • 42
  • 77