Asked me when I was interviewing. If a string is separated by a space, it is actually a very simple question, but I suddenly forget, here is a simple review of the common methods of string and array operations.
String Part
1.split ();
The split () method is used to split a string into an array of strings.
Syntax: Stringobject.split (separator, Howmany)
Parameter: Separator required. A string or regular expression that splits stringobject from where specified by the parameter. An empty string ("") means that each character in the string is split.
Howmany is 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.
Example:
<script type= "Text/javascript" >var str= "How is you doing today?" document.write (Str.split ("") + "<br/>") document.write (Str.split ("") + "<br/>") document.write (str.split ("", 3)) </script>
Output:
How,are,you,doing,today? H,o,w,, A,r,e,, Y,o,u,, D,o,i,n,g,, T,o,d,a,y,? How,are,you
2.slice ();
The slice () method extracts a portion of a string and returns the extracted part with a new string.
Syntax: Stringobject.slice (start, end);
Parameter: Start subscript of the fragment to be extracted. If it is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on.
End subscript at the end of the fragment to be extracted immediately. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string.
Returns a new string for the value. Includes the string Stringobject all characters from start (including start) to end (not including end).
Note that slice if the second argument and the first argument are negative at the same time, the position of end must be after the position of start, not Str.slice ( -5,-6), return an empty string
Example 1, extracts the characters after the sixth bit.
<script type= "Text/javascript" >var str= "Hello Happy world!" document.write (Str.slice (6)) </script>
Output
Happy world!
For example 2, we will extract all the characters from position 6 to position 11:
<script type= "Text/javascript" >var str= "Hello Happy world!" document.write (Str.slice (6)) </script>
Output
Happy
3.subsring ()
The substring () method is used to extract the character of a string intermediary between two specified subscripts.
Grammar stringobject.substring (start, stop);
The substring () and slice () methods are roughly the same, with two differences
1). substring () does not accept a negative parameter
2). If the argument start is equal to stop , then the method returns an empty string (that is, a string of length 0). If start is larger than stop , the method swaps both parameters before extracting the substring.
4.substr ()
The substr () method extracts a specified number of characters from the start subscript in a string.
Syntax: Stringobject.substr (start, length);
Parameter: start required. The starting subscript for the substring to extract. Must be numeric. If it is a negative number, the argument declares the position from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on.
length is optional. The number of characters in the substring. Must be numeric. If this argument is omitted, then the string from the beginning of the Stringobject to the end is returned.
The return value is a new string that contains the length character starting at the start of the Stringobject , including the character that the start refers to. If lengthis not specified, the returned string contains the character from start to the end of stringobject .
Important: ECMAscript does not standardize the method and therefore opposes its use.
Array part
1.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.
Syntax: Arrayobject.join (separator)
Parameters: Separator Optional. Specifies the delimiter to use. If this argument is omitted, a comma is used as the delimiter.
Return value: Returns a string. The string is generated by converting each element of Arrayobject to a string and then connecting the strings to insert a separator string between the two elements.
Example
<script type= "Text/javascript" >var arr = new Array (3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" Document.writ E (Arr.join (".")) </script>
Output
George.John.Thomas
2.slice ()
The slice () method returns the selected element from an existing array.
Syntax: Arrayobject.slice (start, end)
Parameter: 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.
The return value returns a new array containing the elements from start to end (excluding the element) in the Arrayobject. Instead of modifying the array, the method returns a sub-array.
is identical to the slice () method of String.
3.splice ()
The splice () method adds/deletes an item to/from the array, and then returns the item that was deleted. The method changes the original array.
Syntax: Arrayobject.splice (Index, Howmany, item1, ... ItemX)
Parameters: 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.
Item is optional. Adds a new item to the array.
The return value contains the new array of deleted items, if any. If the element is removed from the Arrayobject, the array containing the deleted element is returned.
Example 1 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 "Console.log (arr +" <br/> ") Arr.splice (2,0," William ") Console.log (arr +" <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 "Console.log (arr +" <br/> ") Arr.splice (2,1," William ") Console.log (arr) </ Script>
Output
George,john,thomas,james,adrew,martingeorge,john,william,james,adrew,martin
Example 3 Deleting an element of a 2nd bit value
<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 "Console.log (arr +" <br/> ") arr.splice (2,1) console.log (arr) </script>
Output
George,john,thomas,james,adrew,martingeorge,thomas,james,adrew,martin
The above is a summary of some of the more commonly used methods, and if you need to see more methods for string objects and array objects, you can view the String object method Array Object method
[Interview not answered on question 4] common string and array operations.