如何知道半角或全角字符



我想知道字符串中包含的字符是半角还是全宽。

所以我做了这样的测试:

/* 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

非常感谢!

如果你只想为hankaku-zenkaku配对的字符(例如A)确定这一点,那么它们并不多,像你所做的那样计算它们的范围应该不会太困难。

另一种常见但效率不高的方法是将它们转换为Shift-JIS,并计算产生的字节数:2为全宽,1表示半宽。例如"ア".getBytes("MS932").length

与实际情况一样,这类问题的目的通常是为了输入验证。(即限制或转换其中一个或另一个)。在这种情况下,要处理的字符范围自然是有限的(因为如果不能配对,就无法转换它),并且不需要支持整个Unicode集。

但是,如果您确实想为成熟的Unicode范围执行此操作,那么使用icu4j库获取UCharacter.EastAsianWidth属性可以完成此操作。关于如何走这条路,请参阅以下答案:分析Java 中的全宽或半宽字符

有了数字,您可以使用此代码

/**
* 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);
}

检查全尺寸字符

isFullSizeCharacter(value) {
for (let i = 0; i < value?.length; i++) {
const code = value.charCodeAt(i);
if ((code >= 0x0020 && code <= 0x1fff) || (code >= 0xff61 && code <= 0xff9f)) {
return false;
}
}
return !!(value && 'string' === typeof value) && true;
}

preg_match('/^[^ -~。-゚x00-x1ft]+$/u', $value)

或如何在java中检查字符串是否具有全角字符

最新更新