JS string encoding and Unicode encoding mutual transfer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.