String ss = "我们是害虫";
String sss = "wearepest";
System.out.println(ss.getBytes().length + " | " + ss.length());
System.out.println(sss.getBytes().length + " | " + sss.length());
运行结果为:
10 | 5
9 | 9
if( sss.length() != sss.getBytes().length ) {
//字符串中有中文
} else{
//没中文
}
<script>
function Register(){
if(/[^/u4e00-/u9fa5]/.test(str))
{
alert("你的字符串里不全是中文");
}
}
</script>