Home  >  Article  >  Web Front-end  >  js processing contains Chinese string example sharing

js processing contains Chinese string example sharing

小云云
小云云Original
2018-01-25 11:35:321952browse

This article mainly brings you an example of js processing strings containing Chinese characters. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

Scenario:

#The length attribute of the String type in js obtains the number of characters in the string, but the front-end often It will be necessary to limit the display length of the string. One Chinese character occupies the display position of two English lowercase characters. Therefore, it is often incorrect to use the length value to judge the display length when Chinese and English are mixed.

The conventional solution is to traverse the string. Chinese characters count as length 2, non-Chinese characters count as length 1, and the display length of the string is limited by the newly calculated sum of the lengths. Look at the code↓↓↓


var Tools ={
 //是否包含中文
 hasZh: function(str){
  for(var i = 0;i < str.length; i++)
  {
   if(str.charCodeAt(i) > 255) //如果是汉字,则字符串长度加2
    return true;
   return false;
  }
 },
 //重新计算长度,中文+2,英文+1
 getlen: function(str){
  var strlen = 0;
  for(var i = 0;i < str.length; i++)
  {
   if(str.charCodeAt(i) > 255) //如果是汉字,则字符串长度加2
    strlen += 2;
   else
    strlen++;
  }
  return strlen;
 },
 //限制长度
 limitlen: function(str, len){
  var result = "";
  var strlen = 0;
  for(var i = 0;i < str.length; i++)
  {
   if(str.charCodeAt(i) > 255) //如果是汉字,则字符串长度加2
    strlen += 2;
   else
    strlen++;

   result += str.substr(i,1);

   if(strlen >= len){
    break;
   }
  }
  return result;
 }
}

The principle of this method is based on the different unicode encoding ranges of Chinese and English. Chinese occupies 2 bytes and English occupies 1 Bytes, so the Chinese unicode encoding value must be greater than 2^8-1=255.

The above method can be more rigorous: consider the unicode encoding range, the specific range can be pokedUnicode Table

PS: The unicode encoding range of Chinese characters is 4E00-9FA5 in hexadecimal and 19968-40869 in decimal. That is, the accurate expression for judging Chinese is:


str.charCodeAt(i)>=19968 && str.charCodeAt(i)<=40869

To put in a less rigorous statement, the code does not need to be too restrictive. After all, you don’t know what strange things the user (test) will lose.

Related recommendations:

Implementation method of replacing all jQuery strings

##Detailed explanation of vue syntax splicing strings

Implementation method of js intercepting string function

The above is the detailed content of js processing contains Chinese string example sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn