-2

how do I replace an accent by something else in a string?

I have this :

if (personne1.trim().contains("Président"))

I have to replace the é by anything (this could be e)

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183

2 Answers2

1

This is an example of XY problem. You don't want to replace only é to e but also other literals with diacritics. You want to "normalize" the String. Use the java.text.Normalizer class:

String compared = Normalizer
    .normalize("Président", Normalizer.Form.NFD)
    .replaceAll("[^\\p{ASCII}]", "")

if(personne1.trim().contains(compared)) {
    ...
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
0

String Class has method called replace().

if(personne1.trim().contains("Président")){
   personne1.replace("é", e)
}