search

Home  >  Q&A  >  body text

javascript - Function to count the number of words?

Numbers, letters, and space symbols represent one digit, and Chinese characters represent two digits. How to write a function to calculate the number of digits?

阿神阿神2810 days ago868

reply all(2)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-19 10:15:45

    Use ASCII code to judge

    reply
    0
  • PHP中文网

    PHP中文网2017-05-19 10:15:45

    var i = 0;
    var num=0;
    while (i<str.length) {
        if(str.charCodeAt(i) < 256) {
           num++
        }
        else{
           num+=2;
        }
        i++;
    }
    console.log(num)

    reply
    0
  • Cancelreply