JavaScript Method Example of Obtaining Chinese English Mixed String Length

  • 2021-07-16 01:15:36
  • OfStack

In this paper, an example is given to describe the method of obtaining the length of Chinese and English mixed strings by JavaScript. Share it for your reference, as follows:

JavaScript Method for obtaining the length of Chinese and English mixed strings:

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

Application example:

var fName = file.value.substring(file.value.lastIndexOf('//')+1);
  alert(' File name length cannot exceed 25 Chinese characters 50 Characters ! ');
  return false;

PS: Here are some online character statistics tools for your reference:

Online word count tool:

Online Character Statistics and Editing Tools:

For more readers interested in JavaScript related contents, please check the topics of this site: "Summary of JavaScript Mathematical Operation Usage", "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills" and "Summary of JavaScript Traversal Algorithm and Skills"

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

Related articles: