Hackerss.com

hackerss
hackerss

Posted on

Java Remove accents from a string


import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RemoveAccents {
   /**
   * This method remove accents from a string.
   * @param str String to remove accents.
   * @return String Obtains the string without accents.
   */   
   public static String removeAccents(String str) {
       //Variable to store the string without accents
       String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD); 
       Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
       return pattern.matcher(nfdNormalizedString).replaceAll("");
   }

   /**
   * One Example
   */
   public static void main(String args[]) throws IOException {

      RemoveAccents  removeAccents= new RemoveAccents ();
      //Example
      String str = "Álvaro";
      String strWithoutAccents = removeAccents.removeAccents(str);
      //Output
      System.out.println("String without accents is :" + strWithoutAccents); //Alvaro
   }
}
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)