JavaScript slice (), splice (), split (), substring (), substr ()

Source: Internet
Author: User

Example excerpt from http://www.w3school.com.cn/jsref/jsref_obj_array.asp

1.slice ();

Both array and string objects have

Slice (I,[j]) in array

I is the index value that begins to intercept, the negative number represents the index value from the end, 1 is the first element to the bottom
J is the end of the index value, and the default is to get all elements from I to the end

Parameter returns:
Returns an array of index values from I to J, the original array does not change

In string slice (I,[j])

Parameter description:
I is the index value to start intercepting, the negative number represents the index value from the end, 1 is the first character
J is the end of the index value, and the default is to get all the characters from I to the end

Example 1

In this example, we will create a new array and then display the elements selected from it:

<script type= "Text/javascript" >var arr = new Array (3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" Document.writ E (arr + "<br/>") document.write (Arr.slice (1) + "<br/>") document.write (arr) </script>

  

Output:

George,john,thomasjohn,thomasgeorge,john,thomas

  

Example 2

In this example, we will create a new array and then display the elements selected from it:

<script type= "Text/javascript" >var arr = new Array (6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "Jame S "arr[4] =" Adrew "arr[5] =" Martin "document.write (arr +" <br/> ") document.write (Arr.slice (2,4) +" <br/> ") document.write (arr) </script>

  

Output:

George,john,thomas,james,adrew,martinthomas,jamesgeorge,john,thomas,james,adrew,martin

  

2.splice ()

There is a method in array to add/remove items to/from the array, and then return the item that was deleted. The method will change the original array

Splice (INDEX,HOWMANY,ITEM1,ITEMX)

Index: Required. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.

Howmany: Required. The number of items to delete. If set to 0, the item is not deleted.

ITEM1...ITEMX: Optional. Adds a new item to the array.

The return value array contains the new array of deleted items, if any.

Example 1

In this example, we will create a new array and add an element to it:

<script type= "Text/javascript" >var arr = new Array (6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "Jame S "arr[4] =" Adrew "arr[5] =" Martin "document.write (arr +" <br/> ") Arr.splice (2,0," William ") document.write (arr +" & Lt;br/> ") </script>

  

Output:

George,john,thomas,james,adrew,martingeorge,john,william,thomas,james,adrew,martin

  

Example 2

In this example we will delete the element at index 2 and add a new element to replace the deleted element:

<script type= "Text/javascript" >var arr = new Array (6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "Jame S "arr[4] =" Adrew "arr[5] =" Martin "document.write (arr +" <br/> ") Arr.splice (2,1," William ") document.write (arr) </script>

  

Output:

George,john,thomas,james,adrew,martingeorge,john,william,james,adrew,martin

  

Example 3

In this example we will delete the three elements starting with index 2 ("Thomas") and add a new element ("William") to replace the deleted element:

<script type= "Text/javascript" >var arr = new Array (6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "Jame S "arr[4] =" Adrew "arr[5] =" Martin "document.write (arr +" <br/> ") arr.splice (2,3," William ") document.write (arr) </script>

  

Output:

George,john,thomas,james,adrew,martingeorge,john,william,martin

  

3.split ()

Split in string (Separator,howmany)

Separator: Required. A string or regular expression that splits stringobject from where specified by the parameter.

Howmany: Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.

return value

An array of strings. The array is created by splitting the string stringobject into substrings at the boundary specified by separator. The string of the returned array does not contain the separator itself

However, if separator is a regular expression that contains a subexpression, the returned array includes the strings that match those sub-expressions (but not the text that matches the entire regular expression)

Opposite effect to jion () function

4.substring ()

In string substring (start,stop)

Start: Indicates the starting position of the substring,

Stop: Indicates the end result.

Note: The second parameter should be greater than the first one. If the first argument is greater than the second argument, the substring method automatically replaces the position of the two parameter.

Example 1

In this example, we will use SUBSTRING () to extract some characters from the string:

<script type= "Text/javascript" >var str= "Hello world!" document.write (Str.substring (3)) </script>

  

Output:

Lo world!

  

Example 2

In this example, we will use SUBSTRING () to extract some characters from the string:

<script type= "Text/javascript" >var str= "Hello world!" document.write (str.substring (3,7)) </script>

  

Output:

Lo W

  

5.substr ()

In string, substr (start,length);

Start: The starting position of the substring,

Length: The lengths of substrings.

Example 1

In this example, we will use SUBSTR () to extract some characters from the string:

<script type= "Text/javascript" >var str= "Hello world!" document.write (STR.SUBSTR (3)) </script>

  

Output:

Lo world!

  

Example 2

In this example, we will use SUBSTR () to extract some characters from the string:

<script type= "Text/javascript" >var str= "Hello world!" document.write (Str.substr (3,7)) </script>

  

Output:

Lo Worl

6.replace ()

Definition and usage

The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

Grammar
Stringobject.replace (regexp/substr,replacement)
Parameters Description
Regexp/substr

Necessary. A RegExp object that specifies the substring or pattern to replace.

Note that if the value is a string, it is used as the direct volume text pattern to be retrieved, instead of being converted to the RegExp object first.

Replacement Necessary. A string value. A function that specifies replacement text or generates alternate text.
return value

A new string that is obtained after the first match or all matches of regexp have been replaced with replacement .

Description

The replace () method of the string Stringobject performs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement . If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matched substring.

replacement can be a string, or it can be a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the resulting string from a pattern match will be used for substitution.

character Replace text
$, $ 、...、 The text that matches the 1th to 99th sub-expression in RegExp.
$& A substring that matches the regexp.
$` The text that is located to the left of the matching substring.
$ The text on the right side of the matching substring.
$$ Direct volume symbol.

Note: ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function instead of a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next parameter is a string that matches the subexpression in the pattern and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.

Example 1

In this example, we will replace "Microsoft" in the string with "W3school":

<script type= "Text/javascript" >var str= "Visit microsoft!" document.write (Str.replace (/microsoft/, "W3school")) </script>

  

Output:

Visit w3school!

  

Example 2

In this example, we will perform a global substitution, and whenever "Microsoft" is found, it is replaced with "W3school":

<script type= "Text/javascript" >var str= "Welcome to microsoft! "Str=str +" We are proud to announce that Microsoft have "Str=str +" one of the largest WEB developers sites in the world. " document.write (Str.replace (/microsoft/g, "W3school")) </script>

  

Output:

Welcome to w3school! We is proud to announce this W3schoolhas one of the largest WEB developers sites in the world.

  

Example 3

You can use the code provided in this example to ensure that the matching string is correct for uppercase characters:

Text = "JavaScript Tutorial"; Text.replace (/javascript/i, "JavaScript");

  

Example 4

In this example, we will convert "Doe, John" to the form of "John Doe":

Name = "Doe, John"; Name.replace (/(\w+) \s*, \s* (\w+)/, "$ $");

  

Example 5

In this example, we'll replace all the curly quotes with straight quotes:

name = ' A ', "B" '; Name.replace (/"([^"]*) "/g," ' $ ' ");

  

Example 6

In this example, we will convert all of the words in the string to uppercase in the first letter:

Name = ' aaa bbb ' CCC '; Uw=name.replace (/\b\w+\b/g, function (word) {  return word.substring (0,1). toUpperCase () + Word.substring (1);}  );

  

JavaScript slice (), splice (), split (), substring (), substr ()

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.