Javascript intercepts strings (substring is implemented and supports both Chinese and English)

Source: Internet
Author: User

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 ));-----------""

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.