JS string intercept function slice (), substring (), substr ()

Source: Internet
Author: User
Tags truncated

Summary

In the JS character interception function has commonly used three slice (), substring (), substr (), let me introduce you to the slice (), substring (), substr () function in the character interception of some usage and difference it. Take three functions of a string: Slice (start,[end]), substring (Start,[end]) and substr (Start,[length]) related properties: Slice () The first parameter represents the start position, The second parameter represents the next position of the end position, and the length of the truncated string is the difference between the second parameter and the first parameter, and if the value of the argument is negative, the value is added to the string length, and the null string is returned if the first parameter equals greater than the second argument ...
 

In the JS character interception function has commonly used three slice (), substring (), substr (), let me introduce you to the slice (), substring (), substr () function in the character interception of some usage and difference it.

Three functions for taking a string: Slice (start,[end]), substring (Start,[end]) and substr (Start,[length])
Related properties:

Slice ()
The first parameter represents the start position, the second parameter represents the next position of the end position, the length of the truncated string is the difference between the second parameter and the first parameter, and if the value of the parameter is negative, the value is added to a positive number after the string length, or an empty string if the first argument equals greater than the second argument.

SUBSTRING ()
The first parameter represents the start position, the second parameter represents the next position of the end position, and if the value of the parameter is negative, the value is converted to 0, and the two parameter takes a smaller value as the starting position, and the length of the truncated string is the difference between the larger value and the smaller value.

SUBSTR ()
The first parameter represents the start position, and the second parameter represents the length of the Intercept

PS: Strings are counted starting from 0

Example:

<script type= "Text/javascript" >
var stmp = "rcinn.cn";
Use one parameter
Alert (Stmp.slice (3));//start with the 4th character and intercept to the last character; return "nn.cn"
Alert (stmp.substring (3));//start with the 4th character and intercept to the last character; return "nn.cn"

Use two parameters
Alert (Stmp.slice (1,5))//start from 2nd character to 5th character; return "Cinn"
Alert (stmp.substring (1,5));//start from 2nd character to 5th character; return "Cinn"

If only one parameter is used and 0, then the entire parameter is returned.
Alert (Stmp.slice (0));//returns the entire string
Alert (stmp.substring (0));//returns the entire string

Returns the first character

Alert (Stmp.slice (0,1));//return "R"
Alert (stmp.substring (0,1));//return "R"

In the example above we can see that the usage of slice () and substring () is the same
The returned values are the same, but when the arguments are negative, their return values are different, see the example below
Alert (Stmp.slice (2,-5));//return "I"
Alert (stmp.substring (2,-5));//return "RC"
From the above two examples you can see that slice (2,-5) is actually slice (2,3)
Negative 5 plus string length 8 converts to positive 3 (returns an empty string if the first digit is equal to or greater than the second digit);
While substring (2,-5) is actually substring (2,0), negative conversions to 0,substring always take the smaller number as the starting position.

Alert (stmp.substring (1,5))//start from 2nd character to 5th character; return "Cinn"
Alert (STMP.SUBSTR (1,5));//start with a 2nd character and intercept 5 characters; return "Cinn."

</script>

The difference between substr and substring methods

<script type= "Text/javascript" >
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));//--------""
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));//-------------"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));//-----------""
</script>

Function: Split ()
Function: Use a specified delimiter to store a string into an array
Example:

Str= "Jpg|bmp|gif|ico|png";
Arr=thestring.split ("|");
Arr is an array that contains the character values "JPG", "BMP", "GIF", "ico", and "PNG"

Function: John ()
Function: Merges an array into a string using the delimiter of your choice
Example:

var delimitedstring=myarray.join (delimiter);
var mylist=new Array ("JPG", "BMP", "GIF", "ico", "PNG");
var portablelist=mylist.join ("|");
The result is jpg|bmp|gif|ico|png.

Function: IndexOf ()
Function: Returns the subscript of the first character of a matched substring in a string

var mystring= "JavaScript";
var w=mystring.indexof ("V"); W'll be 2
var x=mystring.indexof ("S"); X would be 4
var y=mystring.indexof ("Script"); y'll also be 4

var z=mystring.indexof ("key"); Z'll be-1

See another very simple method on the Web, the code is as follows:

function func (s, N) {
return S.replace ([[^x00-xff])/g, "$1a"). Slice (0, N). Replace (/([^x00-xff]) a/g, "$1″";
}

This method is very ingenious, and is basically correct. Saying "basically" is because it takes "123 Kanji test" to the left of the 6 substring, it returns "123 Kanji" instead of "123 Han". Of course, this is not necessarily the problem, in some cases the need may be. This method can also be improved, as follows:

function func (s, N) {
Return S.slice (0, N). Replace (/([^x00-xff])/g, "$1a"). Slice (0, N). Replace (/([^x00-xff]) a/g, "$1″";
}

JS string intercept function slice (), substring (), substr ()

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.