check For Non Ascii Chars - Java Internationalization

Java examples for Internationalization:Charset

Description

check For Non Ascii Chars

Demo Code


//package com.java2s;

import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        String resourceName = "java2s.com";
        List strs = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(checkForNonAsciiChars(resourceName, strs));
    }/*from   ww  w.ja v a2  s  .co  m*/

    /**
     * @param resourceName
     * @param strs
     * @return true if the {@code strs} contained non-ASCII characters, false if all characters were ASCII
     */
    public static final boolean checkForNonAsciiChars(String resourceName,
            List<String> strs) {
        boolean res = false;
        for (String str : strs) {
            for (int i = 0, size = str.length(); i < size; i++) {
                if (str.charAt(i) > 127) {
                    System.err.println(resourceName
                            + ": "
                            + i
                            + "'"
                            + str.charAt(i)
                            + "' "
                            + str.substring(Math.max(0, i - 10),
                                    Math.min(str.length(), i + 10)));
                    res = true;
                }
            }
        }
        return res;
    }
}

Related Tutorials