The two-byte character length is automatically computed as 2, and the character is taken as a single byte, as 2 is 1 characters or two letters, and 1 is a Chinese character or letter.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt D "> <ptml xmlns=" http://www.w3.org/1999/xhtml "xml:lang=" en "lang=" en "> <pead> <title></ti tle> </pead> <body> <div id= ' jsdiv ' ></div> <script type= ' text/javascript ' >// Gets the true length of the string (two-byte conversion to two single-byte) function Getstractuallen (schars) {return schars.replace (/[^\x00-\xff]/g, "xx"). Length; //intercept fixed-length substring ssource for string ilen as length function getinterceptedstr (ssource, Ilen) {if Ssource.replace (/[^\x00-\x ff]/g, "xx"). Length <= Ilen) {return ssource; var str = ""; var L = 0; var Schar; for (var i=0; Schar=ssource.charat (i); i++) {str = Schar; L + = (Schar.match (/[^\x00-\xff]/)!= null? 2:1); if (l >= Ilen) {break; } return str; } var str1= "It is a function of a string interception, this is a test!"; Alert (Getstractuallen (STR1)); Alert (GETINTERCEPTEDSTR (str1,25)); </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]