Convert string encoding to Unicode encoding
function Chartounicode (str) {let temp; Let i = 0; Let r = '; Let len = str.length; for (; i < Len; i++) { temp = str.charcodeat (i). toString (+); while (Temp.length < 4) temp = ' 0 ' + temp; R + = ' \\u ' + temp; }; return r;}
Unicode encoding to string encoding
function Unicodetochar (str) { //scheme one return eval ("'" + str + "'"); Scenario Two return unescape (Str.replace (/\u/g, "%u"); }
JS Gets the string length (the true number of characters)//Because the ES5 is preceded by a character "??" which consists of such four bytes. ("??". length = = 2) is processed into 2 lengths, so use the "for of" method to correctly traverse the length of the string function GetLength (str) {let length = 0; For (let Val of str) { length++ } return length }
The Codepointat method is the simplest way to test whether a character is made up of two bytes or four bytes. function Is32bit (c) { return C.codepointat (0) > 0xFFFF;} Is32bit ("??")//Trueis32bit ("ah")//Falseis32bit ("a")//False
In practice, the general design will think that Chinese characters such as ' ah ', ' oh ', ' sum ', ', ' are understood as two lengths, English characters and numbers such as ' A ', ' 1 ', ', ' are understood as a length, so this method can get them to think of the string length (note, not the true length of the string Just the designer understands the length) function getviewlength (str) {let length = 0; For (let C of str) {///note using for of is the length of the string to be correctly traversed, while other methods will "??" As a two-length traverse if (c.codepointat (0) > 0x00FF) {length = length + 2}//either two-byte word such as ' ah ', or four-byte character '??‘,都‘当成‘是属于两个字符长度的范围
' else{length++}} return length}
JS string encoding and Unicode encoding mutual transfer