In this post, we will see how to remove non ascii character from a string in java.
Sometimes, you get non-ascii characters in String and you need to remove them. We will use regular expressions to do it.
Java program to remove non-ascii characters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class StringRemoveASCIIMain { public static void main(String a[]){ String str = "jå∫∆avµa2bl√øog"; System.out.println("Before removing non ASCII characters:"); System.out.println(str); System.out.println("------------------------------------"); // Using regular expressions to remove non ascii characters str = str.replaceAll("[^\p{ASCII}]", ""); System.out.println("After removing non ASCII characters:"); System.out.println(str); } } |
1 2 3 4 5 6 7 |
Before removing non ASCII characters: jå∫∆avµa2bl√øog ------------------------------------ After removing non ASCII characters: java2blog |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.