English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to remove non-ASCII characters from a string

Posix character class\ p {ASCII} Matches ASCII characters, while the meta-character ^ is used as a negative number.

That is, the following expression matches all non-ASCII characters.

"[^\\p{ASCII}]"

replaceAll()The method of the String class accepts a regular expression and a replacement string, and replaces the characters of the current string with the specified replacement string (matching the given pattern).

Therefore, you can usereplaceAll()The method deletes the matched characters by replacing them with an empty string “”.

Example1

import java.util.Scanner;
public class Exp {}}
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      String regex = "[^\\p{ASCII}]";
      System.out.println("Input input data:");
      String input = sc.nextLine();
      String result = input.replaceAll(regex, "");
      System.out.println("Result: "+result);
   }
}

Output Result

Input input data:
Why do we fall
Result: Why do we fall

Example2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter input string:");
      String input = sc.nextLine();
      String regex = "[^\\p{ASCII}]";
      //Create a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Match the compiled pattern in the string
      Matcher matcher = pattern.matcher(input);
      //Create an empty string buffer
      StringBuffer sb = new StringBuffer();
      while (matcher.find()) {
         matcher.appendReplacement(sb, "");
      }
      matcher.appendTail(sb);
      System.out.println("Result: \n");+ sb.toString());
   }
}

Output Result

Enter input string:
Why do we fall
Result:
Why do we fall