JavaScript interception cutting string skills

  • 2020-11-20 06:00:08
  • OfStack

As for the cutting interception of string, it may not be used much at ordinary times, and the segmentation is relatively fine, so you should check by yourself. A preparedness for disaster.

Since all the previous tests were conducted in one demo, please forgive me if I made any mistakes. Some of the remaining properties find time to continue adding.

1. Function: split()

Function: USES a specified delimiter 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 the character values "jpg", "bmp", "gif", "ico", and "png"

2. Function: join()

Function: Combines a number into a string using the delimiter of your choice

Example: var delimitedString= ES43en. join(delimiter);
jpg var myList = new Array (" ", "bmp", "gif", "ico", "png");
var portableList = myList. join (" | ");
// the result is jpg|bmp|gif|ico|png

3. Function :concat ()

Function: Connect two arrays at 1;

Example: arr1 = [1, 2, 3, 4]
arr2 =,6,7,8 [5]
alert(arr1.concat (arr2)) // results [1,2,3,4,5,6,7,8]

4. The function: charAt ()

Function: Returns the character at the specified position. The subscript of the first character in the string is 0. If the parameter index 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(ES111en. charAt(2)) // The result is g

5: function: charCodeAt ()

Functionality: The charCodeAt() method returns the Unicode encoding of the character at the specified position. The return value is an integer between 0 and 65535.

The method charCodeAt() performs the same operation as the charAt() method, except that the former returns the encoding of the character at the specified location, while the latter returns a character substring.

Example: var str = 'a g, i, d, o, v, w, d, k, p'
alert(ES145en. charCodeAt(2)) // the result is 103. The Unicode code for g is 103

6. Function: slice ()

Function: arrayObject. slice (start end)

start: required. Specify where to start. If it is negative, it specifies the position from the end of the array. In other words, minus 1 is the last element, minus 2 is the second last element, and so on.

end: optional. Specify where to end the selection. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the sharded array contains all elements from start to the end of the array. If the argument is negative, it specifies the element that is counted from the end of the array.

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

Example: var str = 'ahji3o3s4e6p8a0sdewqdasj'
alert(str.slice (2,5)) // results ji3

7. Function: substring ()

The substring method is used to extract a string mediation character between two specified subscripts.

Grammar stringObject. substring (start stop)

start required. 1 non-negative integer specifying the position of the first character of the substring to be extracted in stringObject.

stop optional. 1 non-negative integer, 1 more than the last character of the substring to be extracted in stringObject.

If the parameter is omitted, the substring returned will be 1 up to the end of the string.

Returns a new string containing a substring of stringObject, the content of which is all characters from start to stop-1, of length stop minus start. Indicates that the substring returned by the substring method includes characters at start but not end. If start is equal to end, then this method returns an empty string (that is, a string of length 0). If start is larger than end, the 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 1 substring of a specified length starting at a specified location.

Grammar stringObject.substr(start [, length])

Parameter start is required. The starting position of the desired substring. The first character in the string has an index of 0.

length optional. The number of characters that should be included in the returned substring. Indicates that if length is 0 or negative, an empty string will be returned. If this parameter is not specified, the substring continues to the end of 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 (1, 5)); -- -- -- -- -- -- -- -- -- "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>

Related articles: