/**
* 計算字符串長度
* 參數是要計算的字符串
* 返回值當前字符串的長度
*/
function charactersLen (words) {
let tempMapWordNum = {}
let doubleByteTotal = 0 // 雙字節
let chineseTotal = 0 // 漢字
let singleByteTotal = 0 // 單字節
let numTotal = 0
for (let i = 0; i < words.length; i++) {
let c = words.charAt(i)
if (c.match(/[\u4e00-\u9fa5]/)) { // 漢字
if (isNaN(tempMapWordNum[c])) {
tempMapWordNum[c] = 1
}
chineseTotal++
}
}
for (let i = 0; i < words.length; i++) {
let c = words.charAt(i)
if (c.match(/[^\x00-\xff]/)) { // 這裏^表示相反的意思,Ascii碼大於255的那些字符,雙字節的字符
doubleByteTotal++
} else {
singleByteTotal++
}
if (c.match(/[0-9]/)) {
numTotal++
}
}
// 雙字節的內容減去漢字剩下雙字節標點,單字節內容減去數字剩下單字節符號
return chineseTotal + (doubleByteTotal - chineseTotal) + (singleByteTotal - numTotal) + (numTotal)
}ci