Home  >  Article  >  Web Front-end  >  Get the actual length code of Chinese string_javascript skills

Get the actual length code of Chinese string_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:45:591184browse

The default Chinese character length in JS is calculated in the same way as other character lengths, but in some cases we need to get the actual length of the Chinese string. The code is as follows:

Copy Code The code is as follows:

function strLength(str)
{
var realLength = 0, len = str.length, charCode = -1;
for (var i = 0; i < len; i )
{
charCode = str.charCodeAt(i);
if (charCode >= 0 && charCode <= 128)
   {
   realLength = 1;
   }
else
{
realLength = 2; >
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