我希望知道一个字符串中包含的字符是半角还是全角。
因此,我进行了如下测试:
/* 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' ;
}
但是并不适用于所有的全角和半角字符。
所以,您是否有任何建议来解决这个问题呢?
全角和半角通常在亚洲语言中使用,例如日语。
在书写日语字符时,有两种类型的全角和半角。
半角字符 = アデチャエウィオプ
全角字符 = アsdファsヂオpp
非常感谢!