1.正则表达式
/**
* 判断字符串是否为纯数字格式
* @param num
* @return
* @throws BizException
*/
public static boolean verifyNum(String num) throws BizException {
if (num != null && !num.trim().equals( "")) {
Pattern p = Pattern. compile("^[0-9]*$");
Matcher m = p.matcher(num);
boolean b = m.matches();
if (b) {
return true;
}
}
return false;
}
/**
* 判断字符串是否为纯字母格式
* @param num
* @return
* @throws BizException
*/
public static boolean verifyLetter(String str) throws BizException {
if (str != null && !str.trim().equals( "")) {
Pattern p = Pattern. compile("^[A-Za-z]+$");
Matcher m = p.matcher(str);
boolean b = m.matches();
if (b) {
return true;
}
}
return false;
}
/**
* 判断字符串是否为纯字母和数字格式
* @param num
* @return
* @throws BizException
*/
public static boolean verifyLetterAndNum(String str) throws BizException {
if (str != null && !str.trim().equals( "")) {
Pattern p = Pattern. compile("^[A-Za-z0-9]+$");
Matcher m = p.matcher(str);
boolean b = m.matches();
if (b) {
return true;
}
}
return false;
}
2.判断字符串是否包含汉字
public class Test {
public static void main(String[] args) {
// 方法一:
String s1 = "我是中国人" ;
String s2 = "imchinese" ;
String s3 = "im中国人" ;
System. out.println(s1 + ":" + new String(s1).length());
System. out.println(s2 + ":" + new String(s2).length());
System. out.println(s3 + ":" + new String(s3).length());
System. out.println((s1.getBytes(). length == s1.length()) ? "s1无汉字"
: "s1有汉字" );
System. out.println((s2.getBytes(). length == s2.length()) ? "s2无汉字"
: "s2有汉字" );
System. out.println((s3.getBytes(). length == s3.length()) ? "s3无汉字"
: "s3有汉字" );
// 方法二:
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]" ;
String str = "中文fd我是中国人as " ;
Pattern p = Pattern. compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for ( int i = 0; i <= m.groupCount(); i++) {
count = count + 1;
}
}
System. out.println( "共有 " + count + "个 " );
}
}
相关推荐
js常用验证方法,让我们在编程时得心应手,节约大量的时间
常见气体常用检验方法.doc
JS常用验证方法:包括邮箱格式、移动电话、身份证号码、固定电话号码、邮政编码等等常用页面格式的验证。
javascript常用电话、邮编等验证方法
常用验证方法.zip 常用验证方法.zip 常用验证方法.zip 常用验证方法.zip
常用的脚本验证方法常用的脚本验证方法常用的脚本验证方法
超级无敌公共js 常用工具方法 验证 正则表达式
常用统计分析方法 SPSS应用。t检验、相关分析、回归分析、时间序列。
2021年数学建模常用各种检验方法.pdf2021年数学建模常用各种检验方法.pdf
常用原料检验方法的介绍.doc
常用统计分析指标及检验方法,包括四分位数截距,变异系数,t检验,方差检验,F检验,卡方检验等。
包含Javascript的各种常用验证方法
常用方法检验牛奶掺假.doc
CommonMethod(通用操作方法类 1 根据列名字符串数组,创建自定义DataTable CreateSelfDataTable(String[] sList) 2 DES加密解密 3 webform后台js弹层 4 winform后台弹层封装 5 常用正则表达式验证
临床常用免疫学检验方法.pptx
常用方法检验牛奶掺假物.doc
该方法封装了表单验证的方法。引用后使用内置的方法即可。其中使用了正则表达式验证规则。
PCB热设计的检验方法:热电偶 热电现象的实际应用当然是利用热电偶测量温度。电子能量与散射之间的复杂关系,使得不同金属的热电势彼此不同。既然热电偶是这样一种器件,它的两个电极之间的热电势之差是热电偶热端和...
常用方法检验牛奶掺假物总结.doc
NULL 博文链接:https://wangyispider.iteye.com/blog/789423