JS interception and segmentation of strings commonly used techniques summary

  • 2020-10-23 20:01:46
  • OfStack

This article illustrates common JS methods for intercepting and splitting strings. To share for your reference, the details are as follows:

JS can intercept a string using either substring() or slice()

Function: substring ()

Definition: substring(start,end) represents a string from start to end, including characters at start but excluding characters at end.

Feature: string interception. For example, to get "Minidx" from "MinidxSearchEngine", you use substring(0,6).

Example:


var src="images/off_1.png";
alert(src.substring(7,10));
// The pop-up value is: off

Function: substr ()

Definition: substr(start,length) means to intercept an length length string from the start position.

Function: String interception

Example:


var src="images/off_1.png";
alert(src.substr(7,3));
// The pop-up value is: off

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=theString.split("|");
//arr is 1 Contains character values "jpg" , "bmp" , "gif" , "ico" and "png" An array of 

Function: Join ()

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

Example:


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

Function: indexOf ()

Function: Returns the subscript of the first character of a matching substring in a string


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 

Function: lastIndexOf ()

Definition: The lastIndexOf() method returns the first character index of a character or string that appears from right to left (as opposed to indexOf)

Function: Returns a string index value

Example:


var src="images/off_1.png";
alert(src.lastIndexOf('/'));
alert(src.lastIndexOf('g'));
// The pop-up values are in sequence: 6,15

Supplement: Differences between substr and substring methods

substr method

Returns 1 substring of a specified length starting at a specified location.

stringvar.substr(start [, length ])

parameter

stringvar

Will be options. The string literal or String object to extract the substring.

start

Will be options. 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.

instructions

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

The sample

The following example demonstrates the use of the substr method.


function SubstrDemo(){
 var s, ss;    //  Declare variables. 
 var s = "The rain in Spain falls mainly in the plain.";
 ss = s.substr(12, 5); //  Gets the substring. 
 return(ss);    //  return  "Spain" . 
}

substring method

Returns a substring at the specified location in the String object.

strVariable.substring(start, end)
"String Literal".substring(start, end)

parameter

start

Specifies the starting position of the substring, which starts at 0.

end

Indicates the end of the substring, which starts at 0.

instructions

The substring method returns a string containing a substring from start to the end (without end).

The substring method USES the smaller value of both start and end as the starting point for the substring. For example, strvar.substring (0, 3) and strvar.substring (3, 0) will return the same substring.

If start or end is NaN or negative, replace it with 0.

The length of the substring is equal to the absolute value of the difference between start and end. For example, the length of the substring returned in strvar.substring (0, 3) and strvar.substring (3, 0) is 3.

The sample

The following example demonstrates the use of the substring method.


function SubstringDemo(){
 var ss; //  Declare variables. 
 var s = "The rain in Spain falls mainly in the plain..";
 ss = s.substring(12, 17); //  Take a substring. 
 return(ss); //  Returns a substring. 
}

I hope this article has been helpful in JavaScript programming.


Related articles: