replace No ASCII using regex - Java java.util.regex

Java examples for java.util.regex:Match Unicode

Description

replace No ASCII using regex

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String txt = "b\t\nook2s.com";
        System.out.println(replaceNoASCII(txt));
    }//from   w  w w.  ja  v  a 2s  . c om

    /**
     * 
     * @param txt
     * @return Replace all non ascii chars in the string.
     */
    public static String replaceNoASCII(String txt) {
        return txt.replaceAll("[^\\x0A\\x0D\\x20-\\x7E]", "");
    }
}

Related Tutorials