我想知道字符串中包含的字符是半宽或全宽。
所以我测试过这样:
/* 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' ;
}
但是对于所有全宽和半宽字符都不行。
那么,如果您对此问题有任何建议,我可以知道吗?
半角和全宽用于亚洲语言,例如japanese
写日文字符时有两种类型的全宽和半宽。
半角字符=アデチャエウィオプ
全角字符=アsdファsヂオpp
非常感谢!
答案 0 :(得分:1)
使用数字,您可以使用此代码
/**
* Full-angle string conversion half-corner string
* 1, half-width characters are starting from 33 to 126 end
* 2, the full-width character corresponding to the half-width character is from 65281 start to 65374 end
* 3, the half corner of the space is 32. The corresponding Full-width space is 12288
* The relationship between Half-width and Full-width is obvious, except that the character offset is 65248 (65281-33 = 65248).
*
* @param fullWidthStr Non-empty full-width string
* @return Half-angle string
*/
public String halfWidth2FullWidth(String fullWidthStr) {
if (null == fullWidthStr || fullWidthStr.length() <= 0) {
return "";
}
char[] arr = fullWidthStr.toCharArray();
for (int i = 0; i < arr.length; ++i) {
int charValue = (int) arr[i];
if (charValue >= 33 && charValue <= 126) {
arr[i] = (char) (charValue + 65248);
} else if (charValue == 32) {
arr[i] = (char) 12288;
}
}
return new String(arr);
}
答案 1 :(得分:0)
如果你只是想为hankaku-zenkaku配对的字符(例如ア
和ア
,A
和A
)确定这个,那么就没有他们中的很多人并不像你所做的那样制定他们的范围太难了。
另一种常见但不那么有效的方法是将它们转换为Shift JIS并计算生成的字节数: 2 为全宽, 1 为一半-宽度。例如"ア".getBytes("MS932").length
就这种情况而言,这类问题的目的通常是输入验证。 (即限制或转换任何一方或另一方)。在这种情况下,要处理的字符范围自然是有限的(因为如果它不能配对就不能转换它),并且不需要支持整个Unicode集。
如果您确实想要为完全成熟的Unicode范围执行此操作,则使用UCharacter.EastAsianWidth
property获取icu4j library可以执行此操作。请参阅此答案,了解人们如何走这条道路:Analyzing full width or half width character in Java