How to Remove Non-ascii Characters from a String in Java


In this example we will show two methods to remove Non-ascii characters from a string. The first method is to iterate through a string and pull out characters that are ASCII.The second method is to replace non-ascii characters with empty ones.

Source Code

 package com.beginner.examples;

public class RemoveNonAsciiEample {

	public static void main(String[] args) {
		//String to be processed
		String str = "ab丿c丶1丨23";
		
		//Remove using the first method 	
		System.out.println(removeNonAscii(str));
		
		//String to be processed
		String str2 = "z幺x亠v冖n凵m囗";
		
		//Remove using the second method
		System.out.println(removeNonAscii2(str2));

	}

	//Traversal string to extract ASCII characters
	public static String removeNonAscii(String str) {
		
		StringBuilder sb = new StringBuilder();
		
		for (int i = 0; i = 0x00 && c <= 0x7f) {
				sb.append(c);
			}

		}

		return sb.toString();
	}
	
	//Using regular expressions
	public static String removeNonAscii2(String str){
		
		return str.replaceAll("[^\p{ASCII}]", "");
		
	}

}

Output:

abc123
zxvnm
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments