A comparison of Slice, Substring and substr functions of JavaScript intercepting strings

Source: Internet
Author: User
Tags comparison

  This article mainly introduces the slice, Substring and substr functions of JavaScript intercepting strings, which can be referenced by friends in the following

In JavaScript, extracting substrings is mainly done through Slice, Substring, Substr, one of three methods.     Code is as follows://slice //Syntax: String.slice (start [, stop]) "Good news, everyone!". Slice (5,9); //' News '  //substring //Grammar: string.substring (Indexa [, Indexb]) "Good news, everyone!". SUBSTRING (5,9); //' News '  //SUBSTR//Syntax: String.substr (start [, length]) "Good news, everyone!". substr (5,4); //' news   Enter a  start index parameter in three methods, and an optional end index (or length) parameter.   But they are different in some important ways: the 1.substr () method extracts the specified number of characters from the specified location. Param:start begins to extract the position index of the character, length to extract the number of characters. Return: A new string. The length character starting at start. Inconsistent performance in different browsers, modern browsers allow the start index parameter to be negative, to represent the beginning of the string at the end, the number of characters extracted. However, the IE8 and the following versions of the browser start index parameters are calculated at the minimum starting from 0. "Substr the ECMAScript attribute attached to the Web browser, the start index is not recommended when used"     code is as follows: var str = "Abcdefghij";   Console.log ("(1):"      + str.substr (1));    //(1): Bcdefghij console.log ("(1,2):"    + str.substr (1,2));  //(1,2): BC   Console.log ("( -3):" &NBsp   + STR.SUBSTR (-3));    //( -3): Hij console.log ("( -3,2):"   + STR.SUBSTR ( -3,2));  //( -3,2): Hi   Console.log ("(2):"  + str.substr (20,2));  //(2): Console.log ("( -20, 2):" + str.substr ( -20,2)); ( -20, 2): AB      //IE8 and following Console.log ("( -3):" + str.substr (-2)); ( -20, 2): Hij console.log ("( -3, 2):" + str.substr (-2)); ( -20, 2): The AB   2.substring () method is used to extract a subset of the string from one index index to another, or until the end of the string. Param:indexa, Indexb Two arguments take a value range of integers between 0 and the length of the string. Return: Returns a new string, starting from small index to large index, including smaller index position characters, excluding large index position characters. The substring argument is reversible, always using a small parameter value as the start, and a large parameter value as the end. If the argument is less than 0 or Nan, it is considered to be 0, and if the argument is greater than the length of the string, it is considered the length value of the string.   Code is as follows://assumes a print function is defined var anystring = "Mozilla";  //displays "Moz" Console.log (anystring.substring (0,3)); Console.log (anystring.substring (3,0));  //displays "Lla" Console.log (anystring.substring (4,7)); Console.log (anystring.substring (7,4));  //displays "Mozill" Console.log (Anystring.substring (0,6));  //displays "Mozilla" Console.log (anystring.substring (0,7)); Console.log (anystring.substring (0,10));     3.slice extracts part of the string. Param:beginslice begins to extract the position index of the character, can be negative, if the negative value is considered (Sourcelength-beginslice), Sourcelength is the length of the string, That is, the position index of the character to be endslice at the end of the string. If omitted, extract to end. If a negative value is treated as (sourcelength-endslice). Return: Returns a new string of all characters starting at start (including start) and ending with end (excluding ends). Arguments can be negative, and if the index is negative, start at the end of the string.     Code as follows: var str1 = "The Morning is upon us." Console.log (Str1.slice (4,-2));  // morning is upon u   var str = ' The morning is upon us. ' Str.slice (-3);    //"us." Str.slice (-3,-1); "Us" str.slice (0,-1);  //"The Morning is upon us"
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.