The code snippet below remove the characters from a string that is not inside the range of x20
and x7E
ASCII code. The regex below strips non-printable and control characters. But it also keeps the linefeed character n
(x0A
) and the carriage return r
(x0D
) characters.
package org.kodejava.regex;
public class ReplaceNonAscii {
public static void main(String[] args) {
String str = "Thè quïck brøwn føx jumps over the lãzy dôg.";
System.out.println("str = " + str);
// Replace all non ascii chars in the string.
str = str.replaceAll("[^\\x0A\\x0D\\x20-\\x7E]", "");
System.out.println("str = " + str);
}
}
Snippet output:
str = Thè quïck brøwn føx jumps over the lãzy dôg.
str = Th quck brwn fx jumps over the lzy dg.
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024