takes up 3 bytes of rangeU+2e80-u+2ef3:0xe2 0xBA 0x80-0xe2 0xBB 0xb3 A total of 115 u+2f00-u+2fd5:0xe2 0xBC 0x80-0xe2 0xBF 0x95 A total of 213 u+3005- U+3029:0xe3 0x80 0x85-0xe3 0x80 0xa9 Total 36 u+3038-u+4db5:0xe3 0x80 0xb8-0xe4 0xb6 0xb5 A total of 7,549 u+4e00-u+fa6a : 0xE4 0xb8 0x80-0xef 0xa9 0xAA A total of 44,138 u+fa70-u+fad9:0xef 0xa9 0xb0-0xef 0xAB 0x99 Total 105
Total: 52156
Takes up 4 bytes of range
u+20000-u+2fa1d:0xf0 0xA0 0x80 0x80-0xf0 0xAF 0xa8 0x9d a total of 64,029
Total: 64029
So, I think the answer should be:3 or 4 bytes .
Method One: Looping through
Reference: http://www.cnblogs.com/sniper007/p/3309787.html
1 //character encoding value corresponding to the storage length:2 //ucs-2 Encoding (16 binary) UTF-8 byte stream (binary)3 //0000-007f 0xxxxxxx (1 bytes)4 //0080-07ff 110xxxxx 10xxxxxx (2 bytes)5 //0800-ffff 1110xxxx 10xxxxxx 10xxxxxx (3 bytes)6 String.prototype.getBytesLength = function () {7 var totallength = 0; 8 var charcode; 9for (var i = 0; i< This. length; i++) { Ten CharCode= This.charcodeat (i); One if (CharCode < 0x007f) { A totallength++; - } else if ((0x0080 <= charcode)&& (CharCode <= 0x07ff)) { - Totallength += 2; the } else if ((0x0800 <= charcode)&& (CharCode <= 0xFFFF)) { - Totallength += 3; - } else{ - Totallength += 4; + } - } + return totallength; A } at var str= "How are you?" 111? "; - alert ("Number of characters" +str.length+ ", number of bytes" +str.getbyteslength ()); - number of bytes in//java= = = "string". GetBytes ("UTF-8"). Length - //-->
Method Two: Use regular expressions
Ready to be sorted
JS gets the byte length of the string