我想知道字符串中包含的字符是半角还是全角。
所以我这样测试过:
/* Checking typing password is valid or not.
* If length of typing password is less than 6 or
* is greater than 15 or password is composed by full-width character at least one,
* it will return false.
* If it is valid, it will return true.
* @param cmdl
* @param oldPassword
* @return
*/
public boolean isValidNewPassword(String password) {
if ((password.length() < 6)
|| (password.length() > 15) || (isContainFullWidth(password))) {
return false;
}
return true;
}
/**
* Checking full-width character is included in string.
* If full-width character is included in string,
* it will return true.
* If is not, it will return false.
* @param cmdl
* @return
*/
public boolean isContainFullWidth(String cmdl) {
boolean isFullWidth = false;
for (char c : cmdl.toCharArray()) {
if(!isHalfWidth(c)) {
isFullWidth = true;
break;
}
}
return isFullWidth;
}
/**
* Checking character is half-width or not.
* Unicode value of half-width range:
* '\u0000' - '\u00FF'
* '\uFF61' - '\uFFDC'
* '\uFFE8' - '\uFFEE'
* If unicode value of character is within this range,
* it will be half-width character.
* @param c
* @return
*/
public boolean isHalfWidth(char c)
{
return '\u0000' <= c && c <= '\u00FF'
|| '\uFF61' <= c && c <= '\uFFDC'
|| '\uFFE8' <= c && c <= '\uFFEE' ;
}
但并非所有全角和半角字符都可以。
那么,我可以知道您对这个问题有什么建议吗?
半角和全角用于亚洲语言,例如日语
写日文字符时有全角和半角两种类型。
半角字符=アデチャエウィオプ</p>
全角字符 = asdファsヂオppp
非常感谢!