


JavaScript intercepts strings (implemented through substring and supports mixed Chinese and English)_javascript skills
May 16, 2016 pm 05:31 PMJavaScript intercepts strings (supports mixed Chinese and English)
< 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 interception Javascript processing summary (Js interception Chinese string summary)',15))
</script>
Use js method substring()
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()
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));----------""

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of the method of converting int type to string in PHP

How to determine whether a Golang string ends with a specified character

How to check if a string starts with a specific character in Golang?

How to repeat a string in python_python repeating string tutorial

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

PHP string manipulation: a practical way to effectively remove spaces

PHP String Matching Tips: Avoid Ambiguous Included Expressions

PHP techniques for deleting the last two characters of a string
