JavaScript intercepts strings (implemented through substring and supports mixed in Chinese and English) _javascript tips

Source: Internet
Author: User
Tags mixed
JavaScript intercept string (supports mixed in Chinese and English)
Copy Code code 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 intercepts the JavaScript processing summary (JS intercept Chinese string summary) ', 15))
</script>

Using JS method substring ()
Copy Code code as follows:

var str = "0123456789";
Alert (str.substring (0));------------"0123456789"
Alert (str.substring (5));------------"56789"
alert (str.substring);-----------""
alert (str.substring);-----------""
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 ( -1,5));---------"01234"
Alert (str.substring ( -1,-5));--------""

Using JS method substr ()
Copy code code as follows:

var str = "0123456789";
Alert (str.substr (0)),---------------"0123456789"
Alert (STR.SUBSTR (5)),---------------"56789"
Alert ( STR.SUBSTR)--------------""
Alert (STR.SUBSTR),--------------""
Alert (Str.substr ( -5));---------- ----"0123456789"
Alert (Str.substr ( -10)),-------------"0123456789"
Alert (Str.substr ( -12)),-------------" 0123456789
Alert (str.substr (0,5)),-------------"01234"
Alert (Str.substr (0,10)),------------"0123456789"
Alert (STR.SUBSTR (0,12)),------------"0123456789"
Alert (Str.substr (2,0)),-------------""
Alert ( Str.substr (2,2)),-------------"a"
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.