Help us understand the problem. What is going on with this article?

2バイト文字は2として文字列の長さをカウントするGoogleAppsScript関数

More than 1 year has passed since last update.

文字列の長さをカウントする関数

  • GoogleAppsScriptでの文字カウント

 次のようにしても,lenが7になってしまう。

文字数カウント
  var str="ようこそGAP";
  var len = str.len; //lenは7になる
  • 2バイト文字は2としてカウントする関数
文字数カウント
function main(){
  var str="ようこそGAP";
  var len = strLenJ(str); //lenは11になる
}

function strLenJ(str) {//2バイト文字は2として文字数をカウント
  var len = 0;
  str = escape(str);
  for (var i = 0; i < str.length; i++, len++) {
    if (str.charAt(i) == "%") {
      if (str.charAt(++i) == "u") {
        i += 3;
        len++;
      }
      i++;
    }
  }
  return len;
}

参考:http://www.tohoho-web.com/js/string.htm

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away