Skip to content

Instantly share code, notes, and snippets.

@naosim
Created July 26, 2016 07:17
Show Gist options
  • Save naosim/cc1b3a5b5331dccb2ffb468bd1fdd53c to your computer and use it in GitHub Desktop.
Save naosim/cc1b3a5b5331dccb2ffb468bd1fdd53c to your computer and use it in GitHub Desktop.
全角を2、半角を1と数えるコード
// 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