How does a file with Chinese characters know how many bytes to use per character?

前端 未结 9 1650
误落风尘
误落风尘 2020-12-13 05:05

I have read Joel\'s article \"The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)\" but still don\'

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 05:26

    why there are so many complicated answers?

    3 bytes for 1 Chinese character. using this function( under jQuery) :

    function get_length(field_selector) {
      var escapedStr = encodeURI($(field_selector).val())
      if (escapedStr.indexOf("%") != -1) {
        var count = escapedStr.split("%").length - 1
        if (count == 0) count++  //perverse case; can't happen with real UTF-8
        var tmp = escapedStr.length - (count * 3)
        count = count + tmp
      } else {
        count = escapedStr.length
      }
      return count
    }
    

提交回复
热议问题