Techniques for intercepting and cutting strings using JavaScript, javascript strings

Source: Internet
Author: User

Techniques for intercepting and cutting strings using JavaScript, javascript strings

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.

Example: 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.

Example: 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;

Example: arr1 = [1, 2, 4]
Arr2 = [5, 6, 7, 8]
Alert (arr1.concat (arr2) // The result is [1, 2, 3, 4, 5, 6, 7, 8].

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.

Example: 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.

Example: 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 ()

Function: 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.

Example: var str = 'ahji3o3s4e6p8a0sdewqdasj'
Alert (str. slice (2, 5) // 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.

Example: var str = 'ahji3o3s4e6p8a0sdewqdasj'
Alert (str. substring (2, 6) // 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.

Example: 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 (-01234"
Alert (str. substring (-1,-5 ));--------""

Differences between substr and substring Methods

<script type="text/javascript"> 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));//--------"" alert(str.substr(0));//---------------"0123456789" alert(str.substr(5));//---------------"56789" alert(str.substr(10));//--------------"" alert(str.substr(12));//--------------"" alert(str.substr(-5));//--------------"0123456789" alert(str.substr(-10));//-------------"0123456789" alert(str.substr(-12));//-------------"0123456789" alert(str.substr(0,5));//-------------"01234" alert(str.substr(0,10));//------------"0123456789" alert(str.substr(0,12));//------------"0123456789" alert(str.substr(2,0));//-------------"" alert(str.substr(2,2));//-------------"23" alert(str.substr(2,5));//-------------"23456" alert(str.substr(2,12));//------------"23456789" alert(str.substr(2,-2));//------------"" alert(str.substr(-1,5));//------------"01234" alert(str.substr(-1,-5));//-----------"" </script>
Articles you may be interested in:
  • Use javascript to intercept strings that contain Chinese characters for processing.
  • Differences between the substr method and the substring method of JS string Truncation
  • JavaScript uses the split function to intercept strings (obtain the mailbox User Name) according to regulations)
  • Javascript method for obtaining url parameters [Regular Expression and string truncation]
  • Use of the JavaScript substr () string truncation Function
  • Js substring () string truncation Function
  • Common methods and examples of JS string Truncation
  • Common Methods for JS string Truncation
  • Use and comparison of the js string truncation function substr substring slice
  • Three methods for javascript to extract a specified string from the right
  • Slice, Substring, and Substr functions of the JavaScript string are described and compared.

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.