The simplest str.length can be, examples
var str = "465464656464566";
var strlength = str.length;
Example 2, JS statistics string in the number of characters, 1 in Chinese 2 characters example:
<input type= "text" id= "box" size= " /> <input type=" button " onclick=" Countchar () " value=" Statistical characters /> <script type= "Text/javascript" > Function countchar () { var textobj = document.getelementbyid (' box '); var text =
Textobj.value;
var len = 0; for (var i = 0; i < text.length; i++) { if (Text.charcodeat (i) > 127) { len += 2; }
else { len++; } } alert (Len); </script> or regular function getstrlength (str) { var carr = str.match (/[^\x00-\xff]/ig); return str.length + (carr == null ? 0 : carr.length); }
Example 3, distinguishing character encoding
GBK Length calculation function:
&NBSP;GBK Character Set actual length calculation Function getstrleng (str) { var realLength = 0;
var len = str.length;
var charCode = -1; for (var i = 0; i < len; i++) {
charcode = str.charcodeat (i); if (charcode >= 0 && charcode <= 128) {
realLength += 1; }else{ // if it is Chinese then length plus 2
realLength += 2; } }&nbsP
return realLength; }
UTF8 length calculation function:
utf8 Character Set actual length calculation Function getstrleng (str) { var realLength = 0;
var len = str.length;
var charCode = -1; for (var i = 0; i < len; i++) {
charcode = str.charcodeat (i); if (charcode >= 0 && charcode <= 128) {
realLength += 1; }else{ // if it is Chinese then length plus 3
realLength += 3; } }&nbSp
return realLength; }