Checks if a string is pure Ascii - Java java.lang

Java examples for java.lang:String Algorithm

Description

Checks if a string is pure Ascii

Demo Code

/*//from  www. j  a  v a  2 s . c  o m

(C) Copyright 2015-2016 Alberto Fern?ndez <infjaf@gmail.com>
(C) Copyright 2014 Jan Schl??in
(C) Copyright 2003-2004 Anil Kumar K <anil@linuxense.com>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library.  If not, see <http://www.gnu.org/licenses/>.

 */
//package com.java2s;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class Main {
    private static final CharsetEncoder ASCII_ENCODER = Charset.forName(
            "US-ASCII").newEncoder();

    /**
     * Checks if a string is pure Ascii
     * @param stringToCheck the string
     * @return true if is ascci
     */
    public static boolean isPureAscii(String stringToCheck) {
        if (stringToCheck == null) {
            return true;
        }
        return ASCII_ENCODER.canEncode(stringToCheck);
    }
}

Related Tutorials