全角和半角混合文本的字符長度計算


序言

程序前台頁面中,經常有一些有輸入長度限制的input和textarea,限制長度的方法有標簽上加入maxlength屬性和使用js的length屬性獲取輸入的內容長度。
以上的方法適用於大多數情況,但需求更復雜一些,比如輸入框中最多輸入10個全角文字或20個半角文字,即只能輸入10個漢字或者20個英文數字。這時length屬性就變得不適用。

解決方法

將輸入的字符轉為Unicode編碼,根據編碼來判斷哪些是全角字符(對應length+=2),哪些是半角字符(對應length+=1)。

js版本

/**
 * 全角文字的長度
 * @param 輸入文字
 * @return 文字長度
 */
function getMojiLength(str) {

    var realLength = 0;
    var len        = str.length;
    var charCode   = -1;

    for ( var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);

        if (charCode >= 0 && charCode <= 254) {
            // 0-255中的全角文字,依次對應下面的字符
            // ¢ , £ , § , ¨ , « , ¬ , ¯ , ° , ± , ´ , µ , ¶ , · , ¸ , » , × , ÷
            if (charCode == 162
                    || charCode == 163
                    || charCode == 167
                    || charCode == 168
                    || charCode == 171
                    || charCode == 172
                    || charCode == 175
                    || charCode == 176
                    || charCode == 177
                    || charCode == 180
                    || charCode == 181
                    || charCode == 182
                    || charCode == 183
                    || charCode == 184
                    || charCode == 187
                    || charCode == 215
                    || charCode == 247) {
                realLength += 2;
            } else {
                realLength += 1;
            }
        } else if (charCode >= 65377 && charCode <= 65439) {
            if (charCode == 65381) { // '・'該字符的長度為兩個字節
                realLength += 2;
            } else {
                realLength += 1;
            }
        } else {
            realLength += 2;
        }
    }
    return realLength;
}

Java版本

 /**
     * 取得文字的長度
     * @param moji 輸入文字
     * @return 長度
     */
    public static int getMojiLength(String moji) {
        if (isEmpty(moji)) {
            return 0;
        }

        char charCode;
        int mojiLen = 0;
        for (int i = 0; i < moji.length(); i++) {
            charCode = moji.charAt(i);
            if (charCode >= 0 && charCode <= 254) {
                // 0-255中的全角文字
                if (charCode == 162
                        || charCode == 163
                        || charCode == 167
                        || charCode == 168
                        || charCode == 171
                        || charCode == 172
                        || charCode == 175
                        || charCode == 176
                        || charCode == 177
                        || charCode == 180
                        || charCode == 181
                        || charCode == 182
                        || charCode == 183
                        || charCode == 184
                        || charCode == 187
                        || charCode == 215
                        || charCode == 247) {
                    mojiLen += 2;
                } else {
                    mojiLen += 1;
                }
            } else if (charCode >= 65377 && charCode <= 65439) {
                if (charCode == 65381) {
                    mojiLen += 2;
                } else {
                    mojiLen += 1;
                }
            } else {
                mojiLen += 2;
            }
        }
        return mojiLen;
    }

說明

以上的代碼思路是先將問字符轉為Unicode編碼,先判斷是否屬於0-255范圍內,除了幾個特殊的字符是兩個字節,其他為一個字節。接着判斷65377-65439范圍內的長度,65381(對應‘・’字符,占兩個字節),其余是一個字節,除此之外范圍內的字符都是兩個字節。

補充

根據Unicode編碼獲取對應漢字的方法,js為fromCharCode()

var char = "";
var codeArray = [162,163,167,168,171,172,175,176,177,180,181,182,183,184,187,215,247];
for(var i=0; i<codeArray.length; i++){
   char += String.fromCharCode(codeArray[i]) + " , ";
}
//char =  ¢ , £ , § , ¨ , « , ¬ , ¯ , ° , ± , ´ , µ , ¶ , · , ¸ , » , × , ÷

Java寫法更為簡單

int charCode = 162;
String charValue = "" + (char)charCode;
// charValue = ¢

以上介紹的方法還有其他用途,比如文本框中動態追加內容時,要進行合理的換行(兩段的內容長度小於文本框長度,則顯示在同一行,若超出則換行顯示),因為漢字占得位置比較字母數字大,不能根據文字長度來判斷,這時就可以計算文字的真實長度來判斷是否換行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM