Java 如何知道半幅或全幅字符?

Java 如何知道半幅或全幅字符?,java,width,character,Java,Width,Character,我想知道字符串中包含的字符是半宽还是全宽 因此,我进行了如下测试: /* 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

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

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

 /* 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' ;
}
/*检查键入的密码是否有效。
*如果输入密码的长度小于6或
*大于15或密码由至少一个全宽字符组成,
*它将返回false。
*如果它有效,它将返回true。
*@param cmdl
*@param-oldPassword
*@返回
*/
公共布尔值isValidNewPassword(字符串密码){
if((password.length()<6)
||(password.length()>15)| |(isContainFullWidth(password))){
返回false;
}
返回true;
}
/**
*检查字符串中是否包含全宽字符。
*如果字符串中包含全宽字符,
*它会变成现实。
*如果不是,它将返回false。
*@param cmdl
*@返回
*/
公共布尔值isContainFullWidth(字符串cmdl){
布尔值isFullWidth=false;
for(char c:cmdl.toCharArray()){
如果(!isHalfWidth(c)){
isFullWidth=true;
打破
}
}
返回全宽;
}
/**
*检查字符是否为半宽。
*半宽度范围的Unicode值:
*“\u0000”-“\u00FF”
*'\uFF61'-'\uFFDC'
*'\uFFE8'-'\uFFEE'
*如果字符的unicode值在此范围内,
*它将是半宽度字符。
*@param c
*@返回
*/
公共布尔值宽度(字符c)
{

如果您只想确定hankaku-zenkaku配对的字符(例如
A
),它们并不是很多,计算它们的范围应该不会像您所做的那样太困难

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

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


但是,如果您确实希望对成熟的Unicode范围执行此操作,那么使用可以做到这一点。请参阅下面的答案,了解如何走这条路:

使用此代码的编号

    /**
 * 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,半宽字符从33开始到126结束
*2,半宽字符对应的全宽字符从65281开始到65374结束
*3,空间的半角为32。相应的全宽空间为12288
*除了字符偏移量为65248(65281-33=65248)之外,半宽和全宽之间的关系很明显。
*
*@param fullWidthStr非空全宽字符串
*@返回半角字符串
*/
公共字符串半宽2全宽(字符串全宽str){

如果(null==fullWidthStr | | fullWidthStr.length()=33&&charValue什么是“半宽度”字符?什么是“全宽度”字符?同一艘船..这个网站非常详细