Array, string manipulation api

An array of articles:

1 , create an array of

var arrayObj = new Array (); // create an array

var arrayObj = new Array ([size]); // create an array and specified length, attention is not the upper limit, the length

var arrayObj = new Array ([element0 [, element1 [, ... [, elementN]]]]); create an array and assign

    It is noted that, although the second method creates an array of specified length, but in fact an array of all cases are longer, which means that even if you specify a length of 5 , can still be stored outside of the prescribed length of the element, Note: At this length will change.

 

2 , the elements of the array access

var arrValue = arrayObj [1]; // Get the value of the array element

arrayObj [1] = "***"; // to the array element imparting new value

 

3 , add and delete the array elements

. arrayObj Push ([ITEM1 [ITEM2 [[itemN]]]...]); // add one or more new elements to the end of the array, and returns the array length

. arrayObj the unshift ([ITEM1 [ITEM2 [[itemN]]]...]); // add one or more new elements to the array begins, the elements in the array automatic shift, return the array length

. arrayObj POP (); // removes the last element and returns the element value

. arrayObj Shift (); // remove a forwardmost element and returns the element values, elements in the array automatically forward

arrayObj.splice (start, deleteCount, val1, val2, ...); // start removing items from deleteCount start position, and insert val1 from that position, val2, ... 

arrayObj.slice (start, [end]); // return a portion of the array as an array of elements corresponding end note does not include all of the elements will be omitted if after the copy start end

arrayObj.concat ([item1 [, item2 [, [, itemN]]]...]); // plurality of arrays (or a string, array and string or mixed) is connected to an array, good new array return connection

4, a string of array elements

 arrayObj.join (separator); // return a string, each element of the array is connected to the string separator.

 toLocaleString, toString, valueOf: can be seen as a special usage join, and not commonly used

 

5, the array replication

arrayObj.slice (0); // returns a copy of an array of the array, a new array is noted, not directed

arrayObj.concat (); // returns a copy of an array of the array, a new array is noted, not directed

 

6 , sort the array elements 

arrayObj.reverse (); // reversing elements (most discharged before the end of the last routed foremost), returns an array of address

arrayObj.sort (); // sort the array elements, an array of return address

 

 

String articles:

1, the string conversion

was mystr num.toString = ();

was mystr = String (num);

was mystr num + = ''; 

2, character method

stringObject.charAt (index); // Returns the character at that position.

Unicode encoding // Returns the specified character position; stringObject.charCodeAt (index). The return value is 0 - integer between 65535

3, the character string taken ( Slice, the substring, substr)

stringObject.slice (start, end); // extract a string starting at start position, the end position of the end (no end) portions, and returns the new string. Negative predetermined start counting from the end of the string, i.e. the last character -1. If no end, will have to include the extracted start to the end of substrings of the original string string

stringObject.substr (start, length); // extract a string from the start starting at (including the start character referred to)  length  character, if not specified  length , then the returned string comprises from the  start  character to the end of the

stringObject.substring (start, stop); // extracting the specified character string intermediary between the two subscripts. Non-negative integer, from the  start  thereof to  stop all characters -1. If the  start  than  stop  large, then the method before extracting the substring will first swap the two parameters

Method slice (), substring () String object and substr () (deprecated) can return to the specified portion of the string. slice () than the substring () to be flexible, because it allows the use of a negative number as a parameter. slice () and substr () is different because it uses two character positions specify substring, and substr () with the specified character position and length of the substring.

4, the character string into

stringObject.split ( Separator , howmany ); // Separator necessary. String or regular expression, split stringObject from the specified character. howmany Optionally, the specified maximum length of the returned array

5, character string replacement

stringObject.replace ( regexp / substr , Replacement ); // for replacement with other characters in the character string number, or alternatively a substring matching the positive expression. The default were only the first match replacement operation, you want to global replacement, needs to be set on a regular global identity g

6, string pattern matching

(searchValue / stringObject.match  regexp regular expression matching // retrieve the specified values within a string, or to find one or more;).

  (1) directly matching string, the string comprising the string being matched to match, return the string to be matched.

  (2) using a regular matching string, the return value is dependent on the capacitance regexp whether global flag g.

  If regexp no signs G , then the match () method can only be performed once in stringObject the match. If no matching text is not found, match () will return null. Otherwise, it returns an array to store the information and matching text it finds relevant. 0th element of the array is stored in the matching text, while the remaining elements are stored in the regular expression matching sub-expressions of the text. In addition to these conventional elements of the array, the array further comprises two return object properties. index matching property declaration is the position of the starting character in the text of stringObject, input attribute is a reference to a declaration of stringObject.

  If regexp has flag G , the match () method to perform a global search, find all matching substring in stringObject. If you do not find any substring match, null is returned. If one or more matching substring is found, it returns an array. However, the overall return match content with the former very different array, which array elements are stored in all the matching substring stringObject, the property and there is no index or input attributes.

  Details can be found in  the regular matching section.

stringObject.search (regexp); // specified search character string substring, or retrieve the sub-string that matches a regular expression. Returns the string matches the first item of the index, if there is no match, returns -1

7, character string query

stringObject.indexOf (searchvalue, fromindex); // Returns a string substring index occurring at the first (left to right search), if no match, return -1. Case-sensitive.

stringObject.lastIndexOf (searchvalue, fromindex); // Returns a string substring occurring a last index (from right to left search), if no match, return -1. Case-sensitive.

8, a character string to a space

stringObject.trim (); // delete blank characters from both ends of a string. Without affecting the original string itself, it returns a new string.

9, a character string case conversion

stringObject.toLowerCase (); // string to lowercase.

stringObject.toUpperCase (); // string to uppercase.

 

 -- end --

Guess you like

Origin www.cnblogs.com/_error/p/11244942.html