Java String with only ASCII characters
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String s = ""; System.out.println(allAscii(s)); }// www . j a v a2 s . c o m /** * Determines if a string contains only ascii characters */ public static boolean allAscii(String s) { int len = s.length(); for (int i = 0; i < len; ++i) { if ((s.charAt(i) & 0xff80) != 0) { return false; } } return true; } }
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String s = "demo2s.com"; System.out.println(allAscii(s)); }//from www.j av a 2s. c o m /** * Determines if a string contains only ascii characters */ public static boolean allAscii(String s) { int len = s.length(); for (int i = 0; i < len; ++i) { if ((s.charAt(i) & 0xff80) != 0) { return false; } } return true; } }