The comparison _javascript skill of slice, substring and substr of JS string intercepting function

Source: Internet
Author: User

In the JS character intercept function has the commonly used three slice (), the substring (), the substr (), below I will introduce the slice (), the substring (), the substr () function in the character interception some usage and the difference.

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:

 <script type= "Text/javascript" > var stmp = "rcinn.cn"; Use a parameter alert (Stmp.slice (3));//start with the 4th character, intercept to the last character, return "nn.cn" Alert (stmp.substring (3)), start with the 4th character, intercept to the last character, and return " nn.cn "//using two parameter alert (Stmp.slice (1,5))//starting at 2nd character, to 5th character, returning" Cinn "alert (stmp.substring);//starting at 2nd character, to 5th character;
   Cinn "////If only one parameter is used and 0, then the entire argument alert (Stmp.slice (0)) is returned;//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//return The value is the same, but when the argument is negative, their return value is not the same, look at the following example alert (Stmp.slice (2,-5));//return "I" Alert (stmp.substring (2,-5));//return "RC"//From above two examples
   You can see that slice (2,-5) is actually slice (2,3)//minus 5 plus string length 8 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 at the 2nd character, intercepts 5 characters, and returns "Cinn." </ Script> 

The difference between the substr and substring methods

<script type= "Text/javascript" > var str = "0123456789";/alert (str.substring (0));//------------"0123456789" al ERT (Str.substring (5));//------------"56789" alert (str.substring);//-----------"" 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))//----------"(Str.substring (2,2));//----------" "Alert (str.substring (2,5));//------ ----"234" alert (2,12);//---------"23456789" alert (str.substring);---------"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);//------------" 0123456789 "alert (str.substr 2,0);//-------------" "Alert (Str.substr (2,2));//-------------" "" Alert "str.substr ( 2,5))//-------------"23456" alert (STR.SUBSTR (2,12));//------------"23456789" alert (str.substr);//-------- ----"" 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:

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:

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

var mystring= "JavaScript";
var w=mystring.indexof ("V"); W'll be 2
var x=mystring.indexof ("S"); x'll be is 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. 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:

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

The above is the entire content of this article, I hope to learn JavaScript program to help you.

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.