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

Get the actual length code of Chinese string_javascript skills

WBOY
Release: 2016-05-16 16:45:59
Original
1199 people have browsed it

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; >
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