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

Source: Internet
Author: User
Tags split

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

Slice ()
The first parameter represents the start position, and the second parameter represents the next position in the end position. The length of the extracted string is the difference between the second argument and the first parameter, and if the value of the argument is negative, the value is added to the length of the string to positive; if the first argument equals greater than the second argument, an empty string is returned.

SUBSTRING ()
The first parameter represents the start position, the second parameter represents the next position in the end position, and if the argument value is a negative number, the value is converted to 0 two, and the smaller value is taken 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:

The code is as follows Copy Code

<script type= "Text/javascript" >
var stmp = "rcinn.cn";
Use a parameter
Alert (Stmp.slice (3));//starts with the 4th character, intercepts to the last character; returns "Nn.cn"
Alert (stmp.substring (3));//starts with the 4th character, intercepts to the last character; returns "Nn.cn"

Use two parameters
Alert (Stmp.slice (1,5))//begins with the 2nd character, to the 5th character; returns "Cinn"
Alert (stmp.substring (1,5)), starting with the 2nd character, to the 5th character, and returning "Cinn"

If only one argument is used and 0, then the entire argument 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 use of slice () and substring () is the same
The value returned is the same, but when the argument is negative, their return value is not the same, see the following example
Alert (Stmp.slice (2,-5));//return "I"
Alert (stmp.substring (2,-5));//return "RC"
From the above two examples we can see that slice (2,-5) is actually slice (2,3)
Minus 5 plus string length 8 is converted to positive 3 (if the first digit equals or is greater than the second digit, an empty string is returned);
and substring (2,-5) is actually substring (2,0), and negative numbers convert to 0,substring always take the smaller number as the starting position.

Alert (stmp.substring (1,5))//begins with the 2nd character, to the 5th character; returns "Cinn"
Alert (STMP.SUBSTR (1,5));//starts with the 2nd character, intercepts 5 characters, and returns "Cinn."

</script>


the difference between substr and substring methods


Example:

The code is as follows Copy Code
<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: To store a string partition into an array using a specified delimiter
Example:

The code is as follows Copy Code
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 separator of your choice
Example:

The code is as follows Copy Code
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 in a string that matches a substring

The code is as follows Copy Code

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:

The code is as follows Copy Code
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. Say "basically" because it is in the "123 Chinese character test" left the length of 6 of the substring, it returned is "123 Chinese characters", rather than "123 Han". Of course, this is not necessarily the problem, in some cases the demand may be the case. This method can be further improved, as follows:

The code is as follows Copy Code
function func (s, N) {
Return S.slice (0, N). Replace (/([^x00-xff])/g, "$1a"). Slice (0, N). replace (/[[^x00-xff]) a/g, "$1″";
}

Even this improved version, however, is less efficient than the one I put forward with method three because it is simple to write, but sometimes it is more computationally unnecessary.

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.