Created
July 26, 2016 07:17
-
-
Save naosim/cc1b3a5b5331dccb2ffb468bd1fdd53c to your computer and use it in GitHub Desktop.
全角を2、半角を1と数えるコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// copy from http://kihon-no-ki.com/javascript-count-multi-byte-characters-as-two-single-byte-one | |
var charcount = function (str) { | |
len = 0; | |
str = escape(str); | |
for (i=0;i<str.length;i++,len++) { | |
if (str.charAt(i) == "%") { | |
if (str.charAt(++i) == "u") { | |
i += 3; | |
len++; | |
} | |
i++; | |
} | |
} | |
return len; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment