Summary of string functions of JavaScript truncation operations

Source: Internet
Author: User
This article describes how to intercept and cut strings in JavaScript. Common javascript operation string functions include split, join, concat, charAt, charCodeAt, slice, substring, and substr. For more information about coders, see. The string cutting and truncation are usually not used in many cases, and the scores are relatively small. Therefore, you can perform self-check on your own. Be prepared.

Since all of the previous tests were conducted in a demo, we apologize for any errors. Some other attributes can be added later.

1. function: split ()

Function: uses a specified separator to split a string and store it in an array.

Str = "jpg | bmp | gif | ico | png"; arr = str. split ("|"); // arr is an array containing character values "jpg", "bmp", "gif", "ico", and "png"

2. function: join ()

Function: combines an array into a string using the separator you selected.

Var delimitedString = myArray. join (delimiter); var myList = new Array ("jpg", "bmp", "gif", "ico", "png"); var portableList = myList. join ("|"); // The result is jpg | bmp | gif | ico | png

3. function: concat ()

Function: concatenates two arrays;

Arr1 = [,]; arr2 = [,]; alert (arr1.concat (arr2); // The result is [,].

4. function: charAt ()

Function: returns the characters at the specified position. The subscript of the first character in the string is 0. If the index parameter is not between 0 and string. length, this method returns an empty string.

Var str = 'a, g, I, d, o, v, w, d, k, p'; alert (str. charAt (2) // The result is g

5: function: charCodeAt ()

Function: The charCodeAt () method returns the Unicode encoding of characters at the specified position. The returned value is an integer between 0 and 65535.

The charCodeAt () method is similar to the charAt () method, except that the former returns the encoding of the characters at the specified position, and the latter returns the character substrings.

Var str = 'a, g, I, d, o, v, w, d, k, p'; alert (str. charCodeAt (2) // 103 is returned. That is, the Unicode encoding of g is 103.

6. function: slice ()

arrayObject.slice(start,end)

Start: required. Specifies where to start selection. If it is a negative number, it specifies the position starting from the end of the array. That is to say,-1 refers to the last element,-2 refers to the second to last element, and so on.

End: Optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the elements starting from the end of the array.

Returns a new array containing elements in the arrayobject from start to end (excluding this element.

Var str = 'ahji3o3s4e6p8a0sdewqdasj '; alert (str. slice () // result ji3

7. function: substring ()

The substring method is used to extract characters that are intermediate between two specified subscripts.

Syntax

stringObject.substring(start,stop)

Start is required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.

Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringObject.

If this parameter is omitted, the returned substring ends at the end of the string.

Returns a new string. The value of this string contains a substring of stringObject. Its content is all characters from start to stop-1, and its length is stop minus start. It indicates that the substring returned by the substring method includes the start character, but not the end character. If start and end are equal, this method returns an empty string (a string with a length of 0 ). If start is greater than end, this method swaps the two parameters before extracting the substring. If start or end is negative, it is replaced with 0.

Var str = 'ahji3o3s4e6p8a0sdewqdasj '; alert (str. substring () // The result is ji3o3.

8. function: substr

The substr method is used to return a substring of the specified length starting from the specified position.

Syntax

stringObject.substr(start [, length ])

The start parameter is required. The starting position of the required substring. The index of the first character in the string is 0.

Length is optional. The number of characters to be included in the returned substring. If the length is 0 or negative, an empty string is returned. If this parameter is not specified, the substring will continue to the end of the stringObject.

var str = "0123456789";alert(str.substring(0));------------"0123456789"alert(str.substring(5));------------"56789" alert(str.substring(10));-----------"" alert(str.substring(12));-----------"" alert(str.substring(-5));-----------"0123456789" alert(str.substring(-10));----------"0123456789" alert(str.substring(-12));----------"0123456789" alert(str.substring(0,5));----------"01234" alert(str.substring(0,10));---------"0123456789" alert(str.substring(0,12));---------"0123456789" alert(str.substring(2,0));----------"01" alert(str.substring(2,2));----------"" alert(str.substring(2,5));----------"234" alert(str.substring(2,12));---------"23456789" alert(str.substring(2,-2));---------"01" alert(str.substring(-1,5));---------"01234" alert(str.substring(-1,-5));--------""

Differences between substr and substring Methods

Related Article

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.