Home > Web Front-end > JS Tutorial > body text

Summary of methods for obtaining the number of bytes in a string using js

怪我咯
Release: 2017-07-07 10:38:35
Original
2282 people have browsed it

This article mainly introduces the js method of obtaining string bytes. The example summarizes the related techniques of javascript string length calculation. Friends who need it can refer to it

The example in this article describes the method of obtaining the number of bytes in a string using js. Share it with everyone for your reference. The details are as follows:

As we all know, length can be used to obtain the length of a string.
So what about getting the number of bytes of this string?

English letters must have the same length and number of bytes: both are 1
And Chinese length=1, number of bytes=2
Therefore, what needs to be done is to change the number of bytes of Chinese characters Calculated.

Method one:

alert('a'.replace(/[^\u0000-\u00ff]/g,"aaa").length); 
//原理:把中文字符替换成2个英文字母,那么字节数就是2,
//示例中改成替换成3个英文字母了。
//因此弹出的字节数是3,如果要正确的,当然是替换成2个字母了
//\u0000这个表示的是unicode编码
Copy after login

Method two:

var str='我我我';
var bytesCount;
for (var i = 0; i < str.length; i++)
{
  var c = str.charAt(i);
  if (/^[\u0000-\u00ff]$/.test(c)) //匹配双字节
  {
  bytesCount += 1;
  }
  else
  {
  bytesCount += 2;
  }
}
alert(bytesCount);
//结果是6
//原理也很简单,用正则判断是不是中文,如果是的话,字节数就加1。
Copy after login

Regular expression matching Chinese characters: [\u4e00-\u9fa5]

Matches double-byte characters (including Chinese characters): [^\x0000-\x00ff]

can be used to calculate the length of a string (a double-byte character is counted as 2, and ASCII characters are counted as 1) Several functions in

JS:

charAt(num) //Get the character at the num position of the string
charCodeAt(num)// Get the unicode encoding of the character at the num position of the string
fromCharCode(num)//Get the character corresponding to the unicode encoding

The above is the detailed content of Summary of methods for obtaining the number of bytes in a string using js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template