Javascript string and array -- string and array

Source: Internet
Author: User
Tags truncated

I. Methods of the string type:

1. tostring (). This method can accept a parameter, indicating the base number of the output value. For example, VAR num = 10, num. tostring (). In most cases, there is no parameter.

2. conversion rules of the string () function:

This method is called if the value to be converted has the tostring method.

Null and undefined are converted to 'null' and 'undefined respectively.

3. String operation method:

Concat () is used to concatenate a string, for example, var str = 'abc', str. concat ('efg'); // console. log (STR) => 'abcefg ';

Slice (), substring (), returns a New String Based on the string that calls this method. One or two parameters can be accepted. The first parameter indicates the starting position of the truncated string, the second parameter indicates the end position of the truncated string. The returned string contains the character at the starting position, but does not contain the character at the ending position. If there is no second parameter, the end position of a string is used as the end position by default.

Substr () returns a new string based on the characters that call this method. One or two parameters are accepted. The first parameter indicates the starting position of the string to be truncated, the second parameter indicates the number of truncated characters. If there is no second parameter, the end position of a string is used as the end position by default.

The above method will not change the original string

When the parameter is a negative value, slice will add the incoming negative value to the string length, substring will convert all negative values to 0, substr will add the first parameter to the string length, the second negative value is converted to 0.

When the first parameter is greater than the second parameter in slice, an empty string is returned. If the first parameter in substring is greater than the second parameter. By default, the positions of two parameters are exchanged and a string is returned.

Indexof (), lastindexof () query the position of a character or string in the string that calls this method,Same Return Value. If the string does not exist in the call String,-1 is returned. The difference between the two methods is that indexof is searched from the start position of the string, and lastindexof is searched from the end position of the string. Can both accept the optional second parameter, indicating the starting position of the search, one starting from the left and one starting from the right.

Tolowercase () and touppercase () indicate converting the string to lower case and upper case respectively.

4. String Matching Method:

Match () accepts a parameter, a regular expression, or a Regexp object. Returns true or false.

If the parameters accepted by search () are the same as match, the index of the first matching item is returned. -1 is returned if no match exists;

Replace () string replacement. Two parameters are accepted. The first parameter can be a Regexp object or string, and the second parameter can be a string or function. If the first parameter is a string, replace the first match. If you want to replace all self-strings, you must provide a regular expression and specify the global (g) flag.

Split () splits the string and returns an array that can receive one or two parameters. The first parameter represents a separator, which can be a string or a regular expression. The second parameter indicates the length of the returned string.

5. Others

String comparison method: localcompare (); string. localcompare ([String])

String encoding method fromcharcode (), which is a static method: String. fromcharcode ([character encoding])

Charat (); receives a parameter to indicate the character location, and returns the character at that location

Charcodeat (), which accepts a parameter indicating the character position, returns the encoding of the character at this position, opposite to fromcharcode

Ii. Array Method

1. Conversion Method:

Tostring () to convert the array to a string separated by commas (,). [, 3] => '1, 2, 3'

Valueof () returns the value of the array, except [, 3] => [, 3], [, 3,] => [, 3, undefined, undefined]

Join () accepts a parameter and returns a string [1, 2, 3]. Join ('|') => 1 | 2 | 3

2. queue and stack methods

Shift () deletes and returns the first entry of the array,

Unshift () adds an item to the beginning of the array and returns the length of the new array,In IE, undefined is always returned.

Push () adds an item to the end of the array and returns the length of the new array.

Pop () deletes and returns the last item of the array group.

With shift and push, you can implement first-in-first-out queues, while unshift and pop can implement reverse queues.

3. Sorting

Resverse () reverse Array

Sort () will sort the array items in ascending order. This method will call the tostring method of each item in the array and then sort the resulting string. A comparison function can be used as a parameter to facilitate the user to define the sorting method.

4. Operation Method

Concat () merges arrays. This method does not change the original array. When the items in the merged array contain arrays, they are converted to separate items, that is, the converted result is a one-dimensional array.

Slice () creates a new array based on one or more items in the array that calls this method. One or two parameters can be received: the start position and the end position of the truncated array, but they do not contain the end position items. When there is only one parameter, returns the array from this position until the last entry. If the input end position is smaller than the start position, an empty array is returned. When the input parameter is a negative number, the parameter is converted to the sum of the negative values in the array length.This method does not change the original array. This method is similar to the string-type slice method..

Splice () is deleted, inserted, and replaced.

Only two parameters are specified for deletion. the start position of the deletion and the number of items to be deleted.

The insert operation requires at least three parameters. The first parameter indicates the start position, the second 0 parameter indicates the number of deleted items, and the third parameter indicates the items to be inserted. you can insert multiple items, arr. splice (2, 0, 'A', 'B', 'C ');

When the second parameter is not 0, it is replaced: in fact, it is a combination of deletion and insertion.

Splice will change the original array

Indexof () is used to query the position of an array and then in the array. If the group does not contain this item,-1 is returned. lastindexof returns the same result as indexof returns from the last item.The method is the same as that of array.

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.