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

May 16, 2016 pm 05:31 PM
string intercept

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

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 determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

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 check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

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 repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

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 How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

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 manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP string manipulation: a practical way to effectively remove spaces

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions

PHP techniques for deleting the last two characters of a string PHP techniques for deleting the last two characters of a string Mar 23, 2024 pm 12:18 PM

PHP techniques for deleting the last two characters of a string

See all articles