Skip to content

Instantly share code, notes, and snippets.

@laispace
Created November 11, 2015 01:04
Show Gist options
  • Select an option

  • Save laispace/b6b12df3145205df8873 to your computer and use it in GitHub Desktop.

Select an option

Save laispace/b6b12df3145205df8873 to your computer and use it in GitHub Desktop.

Revisions

  1. laispace created this gist Nov 11, 2015.
    17 changes: 17 additions & 0 deletions count-string-length.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    function countStringLength (string) {
    var len = string.length;
    var count = 0;
    for (var i = 0; i < len; i ++) {
    var num = string.charCodeAt(i);
    if (num == 94 || num > 127) {
    count += 2;
    } else {
    count += 1;
    }
    }
    return count;
    }

    countStringLength('abc'); // => 3
    countStringLength('abc啊'); // => 5