
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove non-ASCII characters from strings
The Posix character class \p{ASCII} matches the ASCII characters and the meta character ^ acts as negation.
i.e. The following expression matches all the non-ASCII characters.
"[^\\p{ASCII}]"
The replaceAll() method of the String class accepts a regular expression and a replacement-string and, replaces the characters of the current string (matching the given pattern) with the specified replacement-string.
Therefore, You can remove the matched characters by replacing them with the empty string “, using the replaceAll() method.
Example 1
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("Enter input data:"); String input = sc.nextLine(); String result = input.replaceAll(regex, ""); System.out.println("Result: "+result); } }
Output
Enter input data: whÿ do we fall Result: wh do we fall
Example 2
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}]"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); //Creating 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
Enter input string: whÿ do we fall Result: wh do we fall
- Related Questions & Answers
- JavaScript Remove non-duplicate characters from string
- PHP program to remove non-alphanumeric characters from string
- How can I find non-ASCII characters in MySQL?
- How to remove non-word characters in JavaScript?
- How to remove all non-alphanumeric characters from a string in MySQL?
- How to remove unconvertable characters to ASCII with SELECT in MySQL?
- How to remove non-alphanumeric characters in PHP stirng?
- How to remove unique characters within strings in R?
- How to write Python Regular expression to remove non unicode characters?
- Python – Remove strings with any non-required character
- How to remove empty strings from a list of strings in Python?
- How to Remove Characters from a String in Arduino?
- Remove all non-alphabetical characters of a String in Java?
- How to remove specific characters from a string in Python?
- How to remove characters except digits from string in Python?
Advertisements