Java examples for java.lang:Assert
Test to see if this string contains only US-ASCII (i.e., 7-bit ASCII) Characters.
/*//from w w w . j a v a2s . c om * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ //package com.java2s; public class Main { public static void main(String[] argv) throws Exception { String s = "java2s.com"; System.out.println(isAscii(s)); } /** * Test to see if this string contains only US-ASCII (i.e., 7-bit * ASCII) Characters. * * @param s The test string. * * @return true if this is a valid 7-bit ASCII encoding, false if it * contains any non-US ASCII characters. */ static public boolean isAscii(String s) { for (int i = 0; i < s.length(); i++) { if (!isAscii(s.charAt(i))) { return false; } } return true; } /** * Test to see if a given character can be considered "valid" ASCII. * The excluded characters are the control characters less than * 32, 8-bit characters greater than 127, EXCEPT the CR, LF and * tab characters ARE considered value (all less than 32). * * @param ch The test character. * * @return true if this character meets the "ascii-ness" criteria, false * otherwise. */ static public boolean isAscii(int ch) { // these are explicitly considered valid. if (ch == '\r' || ch == '\n' || ch == '\t') { return true; } // anything else outside the range is just plain wrong. if (ch >= 127 || ch < 32) { return false; } return true; } }