我只知道中英文怎么写,加个数字就不知道怎么办了
[color=blue]String test = "aa";
byte []bytes = test.getBytes();
int i = bytes.length;//i为字节长度
int j = test.length();//j为字符长度
if(i=j){
System.out.println("你输入的是英文")
}else{
System.out.println("你输入的是中文");
}[/color]
[code="java"] String test = "1a";
byte []bytes = test.getBytes();
int i = bytes.length;//i为字节长度
int j = test.length();//j为字符长度
if(Pattern.compile("[0-9]*").matcher(test).matches()){
System.out.println("你输入的是数字") ;
}else if(i==j){
System.out.println("你输入的是英文") ;
}else{
System.out.println("你输入的是中文");
} [/code]
帮你测试好了! :wink:
使用正则判断数字
[code="java"]public boolean isNumeric(String str)
{
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() )
{
return false;
}
return true;
} [/code]
1用JAVA自带的函数
[code="java"]
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
[/code]
2用正则表达式
[code="java"]
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
[/code]
3用ascii码
[code="java"]
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr57)
return false;
}
return true;
}[/code]