java代码判定一个字符串里是否有中英文

 

原理:根据字符串字节的长度和字符串的长度对比

 

 

 

public boolean hasChinese(String str) {
    if (StringUtils.isEmpty(str)) {
        return false;
    }
    //1个英文一个字节,1个 中文2个字节(GBK)
    if (str.getBytes().length == str.length()) {
        return false; //全是英文
    } else {
        return true;
    }
}

猜你喜欢

转载自blog.csdn.net/nakey_xie/article/details/82191657