Some small summaries of string array (beginners can take a look, the use of string array is very important, a lot of useful)

Source: Internet
Author: User

A summary of String Charat

Usage: n = string.charat (String, index);
Passed in is a string and a number;
Column:
var a = String.charat ("World", 2);
var B = String.charat ("World", 0);
var c = String.charat ("World", 10);
The resulting result is
A = "R"
b = "W"
c = ""
The first obtained r after the Word 2 represents, from the beginning of the No. 0 to the second number does not contain the last, the word taken;

Replace

Usage: n = string.replace (String, oldvalue, NewValue);
The string primitive strings. OldValue the value to be replaced. NewValue will replace the value of oldvalue
Column: var a = String.Replace ("World", "wor", "Bo"); var B = string.replace ("World", "LD", "ry");
The resulting result is:
A = "bold"
b = "Worry"
A: Bo instead of wor

Replaceat ()

notation: n = string.replaceat (String, substring, index, separator)
String: The original string.
SUBSTRING: Replaces the string that specifies the element in the string.
Index: An integer that specifies where to replace the substring.
Separator: delimiter.
Column: var a = string.replaceat ("Visit w3school!", "I Love", 0, "");
var B = string.replaceat ("Visit w3school!", "us!", 2, "");
The result is: a = "I love w3school!"
b = "Visit us!"
Use the I love substitution from 0 "" SPACEBAR to start dividing the place end;

SubString ()

notation: n = string.substring (String, start, length)
The string primitive strings.
Start point.
length specifies the lengths of the new string.
Column: var a = string.substring ("World", 2,2);
var B = string.substring ("World", 0,5);
var c = string.substring ("World", -2,3);
var d = string.substring ("World", 2,10);
var e = string.substring ("World", 10,10);
Produce results:
A = "RL"
b = "World"
c = "Wor"
D = "Rld"
E = ""
The reason for the result is that the world character starts with a new array of RL 2 lengths from the 2nd. (the preceding contains.) not included later);

ToString ()

notation: n = string.tostring (value)
The toString () function converts a value to a string.
Column: var a = string.tostring (66);
var B = string.tostring (world);
Result: A = "66"
b = "World"
Converts the number 66 into a string type;

Find ()

notation: n = string.find (String, substring)
The Find () function returns the position of a substring in a string.
string to be retrieved.
Substring the string value to search for in the string.
Example: var a = String.find ("World", "RL");
var B = String.find ("World", "HI");
var c = string.find ("World", "Wo");
The resulting result is: a = 2
b =-1
c = 0
The cause of the result of a = 2 is that the RL is in string world the second beginning of the subscript
The result of B = 1 is that the word is not in the string:

Summary of Arry: Join ()

The join () method is used to put all the elements in an array into a string. The elements are delimited by the specified delimiter.
Wording: Arrayobject.join (separator)
Optional. Specifies the delimiter to use. If this argument is omitted, a comma is used as the delimiter.

<script type="text/javascript">    var arr = new Array(3)    arr[0] = "George"    arr[1] = "John"    arr[2] = "Thomas"    document.write(arr.join())    </script>  

The resulting result is: George,john,thomas:
The result is that an array is declared first, and then the value is added to the array in turn. Finally, call join in the document. This method separates the values in the array into one;

Push ()

The push () method adds one or more elements to the end of the array and returns the new length.
notation: Arrayobject.push (newelement1,newelement2,...., newelementx)
Newelement1 required. The first element to add to the array.
Newelement2 is optional. The second element to add to the array.
Newelementx is optional. You can add multiple elements.

<script type="text/javascript">var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr + "<br />")document.write(arr.push("James") + "<br />")document.write(arr)</script>   

The resulting result is:
George,john,thomas
4
George,john,thomas,james
The reason for the result is. The method of calling push is to add an element to the original array, and when the original array is printed again, the length of the array is incremented and added to the array.

Slice ()

The slice () method returns the selected element from an existing array.
Writing:
Arrayobject.slice (Start,end);
Start Required. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.
End is optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment. If this parameter is not specified, then the segmented array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array.

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

The resulting result is:
George,john,thomas
John,thomas
George,john,thomas
The reason for this result is that when the use of slice is invoked, the input (1) is cut from the No. 0 one,

Splice ()

The splice () method adds/deletes an item to/from the array, and then returns the item that was deleted. Note: This method changes the original array.
notation: Arrayobject.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.

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

The resulting result is:
George,john,thomas,james,adrew,martin
George,john,william,thomas,james,adrew,martin

Some small summaries of string array (beginners can take a look, the use of string array is very important, a lot of useful)

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.