JS interception string commonly used methods collation and use examples

  • 2020-03-26 21:28:24
  • OfStack

Use substring() or slice()

Function: the split ()
Function: stores a string into an array using a specified delimiter
Example:
 
str= " jpg|bmp|gif|ico|png " ; 
arr=theString.split( " | " ); 
//Arr is an array of character values "JPG", "BMP", "GIF", "ico", and "PNG"

John () function:
Function: combine an array into a string using the separator of your choice
Example:
 
var delimitedString=myArray.join(delimiter); 
var myList=new Array( " jpg " , " bmp " , " gif " , " ico " , " png " ); 
var portableList=myList.join( " | " ); 
//Result is JPG | BMP | | GIF ico | PNG

Function: the substring ()
Function: string interception. For example, substring(0,6) is used to get "Minidx "from "MinidxSearchEngine".

Function: indexOf ()
Function: returns the subscript of the first character in a string that matches a substring
 
var myString= " JavaScript " ; 
var w=myString.indexOf( " v " );w will be 2 
var x=myString.indexOf( " S " );x will be 4 
var y=myString.indexOf( " Script " );y will also be 4 
var z=myString.indexOf( " key " );z will be -1 

Follow-up:

1. The substring method

Definition and usage
The substring method is used to extract the characters of a string mediation between two specified subscripts.

grammar
StringObject. Substring (start, stop)

Parameters to describe
Start a necessity. A non-negative integer that specifies the position in the stringObject of the first character of the substring to be extracted.
The stop is optional. A non-negative integer, one more than the position of the last character of the substring to be extracted in the stringObject. If you omit this parameter, the returned substring will go all the way to the end of the string.

The return value
A new string whose value contains a substring of stringObject that contains all characters from start to stop-1, with a length of stop minus start.

instructions
The substring method returns a substring that includes the character at start, but not the character at end.
If start is equal to end, the method returns an empty string (that is, a string of length 0).
If start is larger than end, the method exchanges the two parameters before extracting the substring.
If start or end is negative, it will be replaced with 0.

2. The substr method

Definition and usage
The substr method returns a substring of the specified length starting at the specified location.

grammar
StringObject. Substr (start [, length])

Parameters to describe
Start a necessity. The starting position of the desired substring. The index of the first character in a string is 0.
Length optional. The number of characters to include in the returned substring.

instructions
If the length is 0 or negative, an empty string is returned.
If this parameter is not specified, the substring continues to the end of the stringObject.

For 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));--------"" 

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));-----------"" 

Related articles: