How do I detect non-ASCII characters in string?

The code below detect if a given string has a non ASCII characters in it. We use the CharsetDecoder class from the java.nio package to decode string to be a valid US-ASCII charset.

package org.kodejava.example.nio;

import java.nio.charset.CharsetDecoder;
import java.nio.charset.Charset;
import java.nio.charset.CharacterCodingException;
import java.nio.CharBuffer;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class NonAsciiValidation {
    public static void main(String[] args) {
        // This string contains a non ASCII character which will produce exception
        // in this program. While the second string has a valid ASCII only chars.
        byte[] invalidBytes = "Copyright © 2005-2009 Kode Java Org".getBytes();
        byte[] validBytes = "Copyright (c) 2005-2009 Kode Java Org".getBytes();

        // Returns a charset object for the named charset.
        CharsetDecoder decoder = Charset.forName("US-ASCII").newDecoder();
        try {
            CharBuffer buffer = decoder.decode(ByteBuffer.wrap(validBytes));

            buffer = decoder.decode(ByteBuffer.wrap(invalidBytes));
        } catch (CharacterCodingException e) {
            System.err.println("The information contains a non ASCII character(s).");

Below is the result of the program:

[C, o, p, y, r, i, g, h, t,  , (, c, ),  , 2, 0, 0, 5, -, 2, 0, 0, 9,  , K, o, d, e,  , J, a, v, a,  , O, r, g]
The information contains a non ASCII character(s).
java.nio.charset.MalformedInputException: Input length = 1
	at java.nio.charset.CoderResult.throwException(
	at java.nio.charset.CharsetDecoder.decode(
	at org.kodejava.example.nio.NonAsciiValidation.main(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.intellij.rt.execution.application.AppMain.main(

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply