android 判断字符串是否为数字或中文或字母
2017-06-23 00:00
627 查看
http://hi.baidu.com/dillisbest/blog/item/880021fbe9ae148c58ee90f8.html
1.判断字符串是否仅为数字:
1>用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
2>用正则表达式
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
3>用ascii码
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
2.判断一个字符串的首字符是否为字母
public static boolean test(String s)
{
char c = s.charAt(0);
int i =(int)c;
if((i>=65&&i<=90)||(i>=97&&i<=122))
{
return true;
}
else
{
return false;
}
}
public static boolean check(String fstrData)
{
char c = fstrData.charAt(0);
if(((c>='a'&&c<='z') || (c>='A'&&c<='Z')))
{
return true;
}else{
return false;
}
}
3 .判断是否为汉字
public boolean vd(String str){
char[] chars=str.toCharArray();
boolean isGB2312=false;
for(int i=0;i<chars.length;i++){
byte[] bytes=(""+chars[i]).getBytes();
if(bytes.length==2){
int[] ints=new int[2];
ints[0]=bytes[0]& 0xff;
ints[1]=bytes[1]& 0xff;
if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){
isGB2312=true;
break;
}
}
}
return isGB2312;
}
Pattern p = Pattern.compile("[0-9]*");
Matcher m = p.matcher(txt);
if(m.matches() ){
Toast.makeText(Main.this,"输入的是数字", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[a-zA-Z]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是字母", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[\u4e00-\u9fa5]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是汉字", Toast.LENGTH_SHORT).show();
}
1.判断字符串是否仅为数字:
1>用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
2>用正则表达式
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
3>用ascii码
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
2.判断一个字符串的首字符是否为字母
public static boolean test(String s)
{
char c = s.charAt(0);
int i =(int)c;
if((i>=65&&i<=90)||(i>=97&&i<=122))
{
return true;
}
else
{
return false;
}
}
public static boolean check(String fstrData)
{
char c = fstrData.charAt(0);
if(((c>='a'&&c<='z') || (c>='A'&&c<='Z')))
{
return true;
}else{
return false;
}
}
3 .判断是否为汉字
public boolean vd(String str){
char[] chars=str.toCharArray();
boolean isGB2312=false;
for(int i=0;i<chars.length;i++){
byte[] bytes=(""+chars[i]).getBytes();
if(bytes.length==2){
int[] ints=new int[2];
ints[0]=bytes[0]& 0xff;
ints[1]=bytes[1]& 0xff;
if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){
isGB2312=true;
break;
}
}
}
return isGB2312;
}
android判断EditText输入的数字、中文还是字母方法
String txt = edInput.getText().toString();Pattern p = Pattern.compile("[0-9]*");
Matcher m = p.matcher(txt);
if(m.matches() ){
Toast.makeText(Main.this,"输入的是数字", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[a-zA-Z]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是字母", Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[\u4e00-\u9fa5]");
m=p.matcher(txt);
if(m.matches()){
Toast.makeText(Main.this,"输入的是汉字", Toast.LENGTH_SHORT).show();
}
相关文章推荐
- Android判断字符串中是否含字母、中文或数字
- android 判断字符串是否为数字或中文或字母 分类: Android JAVA 2015-06-07 21:07 29人阅读 评论(0) 收藏
- android 判断字符串是否为数字或中文或字母
- java判断字符串是否为数字或中文或字母
- java判断字符串是否为数字或中文或字母
- java判断字符串是否为数字或中文或字母
- java判断字符串是否为数字或中文或字母
- java判断字符串是否为数字或中文或字母
- Android 判断字符串是数字、中文还是字母的方法
- android 判断字符串是否同时包含小写字母与数字
- 判断字符串是否包含字母、数字、中文
- 判断字符串是否为数字、中文、字母
- java判断字符串是否为数字或中文或字母
- 判断字符串是否为数字或中文或字母
- java判断字符串是否为数字或中文或字母
- java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字 分类: Android JAVA 2015-06-07 21:08 31人阅读 评论(0) 收藏
- android判断EditText输入的数字、中文还是字母方法
- php如何判断字符串是否是字母和数字的组合
- java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字
- java判断字符串是否全部由数字,大小写字母,特殊符号组成