【发布时间】:2012-11-13 14:06:03
【问题描述】:
我想知道字符串中包含的字符是半角还是全角。
所以我已经这样测试了:
/* 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
非常感谢!
【问题讨论】:
-
什么是“半角”字符?什么是“全角”字符?
-
同一条船.. 虽然solutions.asia/2011/05/…这个网站非常详细