來源:http://itssh.cn/post/943.html
JAVA實現(xiàn)連續(xù)字母或者數(shù)字:
實現(xiàn)思路:統(tǒng)一轉(zhuǎn)成ASCII進行計數(shù)判斷哥倔,純數(shù)字、純字母
//純數(shù)字(數(shù)字0 -- 數(shù)字9,對應(yīng)ASCII為48 -- 57)
//大寫純字母(大寫字母A -- 大寫字母Z,對應(yīng)ASCII為65 -- 90)
//小寫純字母(小寫字母a -- 小寫字母z捞蛋,對應(yīng)ASCII為97 -- 122)
案例:
package cn.sunmeng.utils;
/**
* @ClassName: SimpleLetterAndNumCheck.java
* @Description: JAVA實現(xiàn)連續(xù)字母或者數(shù)字:<br/>
實現(xiàn)思路:統(tǒng)一轉(zhuǎn)成ASCII進行計數(shù)判斷,純數(shù)字、純字母<br/>
//純數(shù)字(數(shù)字0 -- 數(shù)字9,對應(yīng)ASCII為48 -- 57)<br/>
//大寫純字母(大寫字母A -- 大寫字母Z,對應(yīng)ASCII為65 -- 90)<br/>
//小寫純字母(小寫字母a -- 小寫字母z饲嗽,對應(yīng)ASCII為97 -- 122)<br/>
* @author: SM(sm0210@qq.com)
* @date: 2017年5月28日 上午10:37:22
*/
public class SimpleLetterAndNumCheck {
/**
* SM 校驗字符串連續(xù)多少位是純數(shù)字或者純字母,默認6位(字母區(qū)分大小寫)
* @param password 密碼
* @return
*/
public static boolean simpleLetterAndNumCheck(String value){
//
return SimpleLetterAndNumCheck.simpleLetterAndNumCheck(value, 6);
}
/**
* SM 校驗字符串連續(xù)多少位是純數(shù)字或者純字母奈嘿,默認6位(字母區(qū)分大小寫)
* @param password 密碼
* @param length 校驗長度,默認6為
* @return
*/
public static boolean simpleLetterAndNumCheck(String value, int length){
//是否不合法
boolean isValidate = false;
//
int i = 0;
//計數(shù)器
int counter = 1;
//
for(; i < value.length() -1;) {
//當(dāng)前ascii值
int currentAscii = Integer.valueOf(value.charAt(i));
//下一個ascii值
int nextAscii = Integer.valueOf(value.charAt(i + 1));
//滿足區(qū)間進行判斷
if( (SimpleLetterAndNumCheck.rangeInDefined(currentAscii, 48, 57) || SimpleLetterAndNumCheck.rangeInDefined(currentAscii, 65, 90) || SimpleLetterAndNumCheck.rangeInDefined(currentAscii, 97, 122))
&& (SimpleLetterAndNumCheck.rangeInDefined(nextAscii, 48, 57) || SimpleLetterAndNumCheck.rangeInDefined(nextAscii, 65, 90) || SimpleLetterAndNumCheck.rangeInDefined(nextAscii, 97, 122)) ) {
//計算兩數(shù)之間差一位則為連續(xù)
if(Math.abs((nextAscii - currentAscii)) == 1){
//計數(shù)器++
counter++;
}else{
//否則計數(shù)器重新計數(shù)
counter = 1;
}
}
//滿足連續(xù)數(shù)字或者字母
if(counter >= length) return !isValidate;
//
i++;
}
//
return isValidate;
}
/**
* SM 判斷一個數(shù)字是否在某個區(qū)間
* @param current 當(dāng)前比對值
* @param min 最小范圍值
* @param max 最大范圍值
* @return
*/
public static boolean rangeInDefined(int current, int min, int max) {
//
return Math.max(min, current) == Math.min(current, max);
}
/**
*
* @param args
*/
public static void main(String[] args) {
//
//String str = "1234677A!@#B0abcdeg123456DDzZ09";
//連續(xù)不合法區(qū)間值校驗
String str = ":;<=>?@A";
//
boolean validate = SimpleLetterAndNumCheck.simpleLetterAndNumCheck(str);
//
if(validate){
System.out.println("連續(xù)字母或者數(shù)字");
}else {
System.out.println("合法的校驗");
}
}
}