Javascript intercepts strings (both Chinese and English are supported)
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var sub = function (str, n ){
Var r =/[^ \ x00-\ xff]/g;
If (str. replace (r, "mm"). length <= n) {return str ;}
Var m = Math. floor (n/2 );
For (var I = m; I <str. length; I ++ ){
If (str. substr (0, I). replace (r, "mm"). length> = n ){
Return str. substr (0, I) + "...";
}
}
Return str;
}
Alert (sub ('string truncation Javascript processing summary (Js truncation Chinese string summary) ', 15 ))
</Script>
Substring () using js method ()
Copy codeThe Code is as follows:
Var str = "0123456789 ";
Alert (str. substring (0); ------------ "0123456789"
Alert (str. substring (5); ------------ "56789"
Alert (str. substring (10 ));-----------""
Alert (str. substring (12 ));-----------""
Alert (str. substring (-5); ----------- "0123456789"
Alert (str. substring (-10); ---------- "0123456789"
Alert (str. substring (-12); ---------- "0123456789"
Alert (str. substring (0, 5); ---------- "01234"
Alert (str. substring (0, 10); --------- "0123456789"
Alert (str. substring (0, 12); --------- "0123456789"
Alert (str. substring (2, 0); ---------- "01"
Alert (str. substring (2, 2 ));----------""
Alert (str. substring (2, 5); ---------- "234"
Alert (str. substring (2, 12); --------- "23456789"
Alert (str. substring (2,-2); --------- "01"
Alert (str. substring (-01234"
Alert (str. substring (-1,-5 ));--------""
Substr () using js method ()
Copy codeThe Code is as follows:
Var str = "0123456789 ";
Alert (str. substr (0); --------------- "0123456789"
Alert (str. substr (5); --------------- "56789"
Alert (str. substr (10 ));--------------""
Alert (str. substr (12 ));--------------""
Alert (str. substr (-5); -------------- "0123456789"
Alert (str. substr (-10); ------------- "0123456789"
Alert (str. substr (-12); ------------- "0123456789"
Alert (str. substr (01234"
Alert (str. substr (0, 10); ------------ "0123456789"
Alert (str. substr (0, 12); ------------ "0123456789"
Alert (str. substr (2, 0 ));-------------""
Alert (str. substr (2, 2); ------------- "23"
Alert (str. substr (2,5); ------------- "23456"
Alert (str. substr (2,12); ------------ "23456789"
Alert (str. substr (2,-2 ));------------""
Alert (str. substr (-1, 5); ------------ "01234"
Alert (str. substr (-1,-5 ));-----------""