try:<%
String tempStr = "abcd你好01239";
byte[] tempb= tempStr.getBytes() ;
for (int i=0;i<tempb.length;i++){
if (tempb[i]<0)
out.println("=="+tempb[i]+"是汉字<br>") ;
else if (tempb[i]>=48&&tempb[i]<=57)
out.println("=="+tempb[i]+"是数字<br>") ;
else
out.println("=="+tempb[i]+"是字母<br>") ;
}
%>用byte判断时,一个汉字为两个字节。高位都为1。
String tempStr = "abcd你好01239";
byte[] tempb= tempStr.getBytes() ;
for (int i=0;i<tempb.length;i++){
if (tempb[i]<0)
out.println("=="+tempb[i]+"是汉字<br>") ;
else if (tempb[i]>=48&&tempb[i]<=57)
out.println("=="+tempb[i]+"是数字<br>") ;
else
out.println("=="+tempb[i]+"是字母<br>") ;
}
%>用byte判断时,一个汉字为两个字节。高位都为1。
<%@ page import="java.io.*,java.util.*"%>
<%!
public boolean isHave(String str){
int l=str.length();
for (int i=0;i<l;i++){
if (str.charAt(i)<0||str.charAt(i)>255) return true;
}
return false;
}
%>
<%
String str="sdfsdf";
out.println(str+":"+isHave(str)+"<p>");
str="中国";
out.println(str+":"+isHave(str)+"<p>");
%>
{
return (('0'<=cCheck) && (cCheck<='9'));
}function IsAlpha(cCheck)
{
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))
}function OkFunction(form1){
for (nIndex=0; nIndex<document.form1.txt1.value.length; nIndex++)
{
cCheck = document.form1.txt1.value.charAt(nIndex);
if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))
{
alert("用户帐号只能使用字母、数字以及_,并且不能使用中文");
document.form1.txt1.focus();
return false;
}
}
}
{
boolean isCharacter = false;
try
{
byte[] bb=str.getBytes("UTF16");
for(int i=2;i<bb.length;i+=2)
{
if(bb[i]!=0){
isCharacter = true;
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return isCharacter;
}
返回為true時有漢字