Method one: Using the Escape function
collapse Expand JavaScript code copy content to clipboard
- function Cnlength (STR) {
- var escstr = Escape (STR);
- var Numi = 0;
- var escstrlen = escstr.length;
- For (i = 0; i < Escstrlen; i++)
- if (Escstr.charat (i) = = '% ')
- if (Escstr.charat (++i) = = ' u ')
- numi++;
- return str.length + Numi;
- }
Method Two: Use regular match Chinese, replace each Chinese with two comma to calculate the length
collapse Expand JavaScript code copy content to clipboard
- function GetLength (str) {
- return Str.replace (/[^\x00-\xff]/g, ' ... '). Length
- }
Method Three: Determine the number of bytes
collapse Expand JavaScript code copy content to clipboard
- function Fucchecklength (strtemp) {
- var i, sum;
- sum = 0;
- For (i = 0; i < strtemp.length; i++) {
- if ((Strtemp.charcodeat (i) >= 0) && (strtemp.charcodeat (i) <= 255)) {
- sum = sum + 1;
- } Else {
- sum = sum + 2;
- }
- }
- return sum;
- }
The solution of mixed string length in JAVASCRIPT verification calculation