如何判断半角或全角字符?

4

我希望知道一个字符串中包含的字符是半角还是全角。

因此,我进行了如下测试:

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

非常感谢!


1
什么是“半角”字符?什么是“全角”字符? - Jim Garrison
同舟共济。不过这个网站非常详细,可以参考一下:http://www.solutions.asia/2011/05/half-and-full-width-characters-in-cjk.html - antak
3个回答

1
如果您只想确定汉字是否为半角或全角(例如A),那么这些字符并不多,像你所做的那样计算它们的范围应该不难。

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

通常此类问题的目的是进行输入验证(即限制或转换其中之一)。在这种情况下,要处理的字符范围自然是有限的(因为如果无法配对,则无法进行转换),而且不需要支持整个Unicode集。

如果您想对完整的Unicode范围进行此操作,可以使用icu4j库获取UCharacter.EastAsianWidth属性来实现。请参见此答案了解如何进行此操作:在Java中分析全角或半角字符。

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

0

检查全尺寸字符

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-\x1f\t]+$/u', $value)

如何在Java中检查字符串是否含有全角字符


请参见“完全基于代码的答案解释”。虽然这可能在技术上是正确的,但它并没有解释为什么它可以解决问题或应该被选为最佳答案。我们应该在帮助解决问题的同时进行教育。 - the Tin Man

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接