------- android培训、java培训、期待与您交流! ----------
/*正则表达式:符合一定规则的表达式
作用:用于专门操作字符串
*/
class RegexDemo
{
public static void main(String[] args)
{
checkQQ();
}
public static void checkQQ()
{
String qq = "2345";
String regex = "[1-9][0-9]{4,14}";
boolean flag = qq.matches(regex);
if (flag)
{
System.out.println(qq+"...is ok");
}
else
{
System.out.println(qq+"....不合法");
}
}
/*
对qq号码进行校验
要求:5-15位 o不能开头 ,只能是数字
这种方式: 使用了String 类中的方法,进行组合完成需求,但是代码过于复制。
*/
public static void checkQQ_1()
{
String qq= "1343333333333333335";
int len = qq.length();
if (len>=5 && len<=15)
{
if(!qq.startsWith("0"))
{
try
{
long l = Long.parseLong(qq);
System.out.println("qq:"+l);
}
catch (NumberFormatException e )
{
System.out.println("出现非法字符");
}
/*
char[] arr = qq.toCharArray();
boolean flag = true;
for (int x=0;x<arr.length ;x++ )
{
if(!(arr[x]>='0' && arr[x]<='9'))
{
flag = false;
break;
}
}
if (flag)
{
System.out.println("qq:"+qq);
}
else
{
System.out.println("出现非法字符");
}
*/
}
else
{
System.out.println("不可以以0开头");
}
}
else
{
System.out.println("长度错误");
}
}
}