Home > Web Front-end > JS Tutorial > JavaScript intercepts strings (implemented through substring and supports mixed Chinese and English)_javascript skills

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

WBOY
Release: 2016-05-16 17:31:30
Original
1325 people have browsed it

JavaScript intercepts strings (supports mixed Chinese and English)

Copy code The 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;iif(str.substr(0,i).replace(r,"mm").length>=n){
return str.substr(0,i) "...";
}
}
return str;
}
alert(sub('String interception Javascript processing summary (Js interception Chinese string summary)',15))


Use js method substring()
Copy code The 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(-1,5));---------"01234"
alert(str.substring(-1,-5));--------""

Use js method substr()
Copy code The 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(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));-------------"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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template