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
)
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
)
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)) {
...
}
String
Class has method called replace()
.
if(personne1.trim().contains("Président")){
personne1.replace("é", e)
}