SoFunction
Updated on 2025-04-10

Example of JavaScript to obtain the length of mixed strings in Chinese and English

This article describes the method of obtaining the length of mixed strings in Chinese and English in JavaScript. Share it for your reference, as follows:

JavaScript to get the length of a mixed string in Chinese and English:

function StrLen(sString) {
  var j = 0;
  var s = sString;
  if (s=="") return j;
  for (var i=0; i<; i++)
  {
   if ((i,1).charCodeAt(0)>255) j = j + 2;
   else j++
  }
  return j;
}

Application example:

var fName = (('//')+1);
if(StrLen(fName)&gt;50){
  alert('The length of the file name cannot exceed 25 Chinese characters and 50 characters! ');
  return false;
}

PS: Here are a few online character statistics tools for your reference:

Online word count tool:
http://tools./code/zishutongji

Online character statistics and editing tools:
http://tools./code/char_tongji

For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript mathematical operations usage》、《Summary of json operation skills in JavaScript》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques"and"Summary of JavaScript traversal algorithm and skills

I hope this article will be helpful to everyone's JavaScript programming.