1. substring()Method is used to extract characters between two specified subscripts in a (string). let a = "1,2,3"; document.write(a.substring(2,a.Length)) //The first parameter starts from the character with subscript 0, including the current subscript 0, //The second parameter ends at the character with the subscript, excluding the current subscript. By default, it is +1 longer than the subscript position of the last character of the substring to be extracted in the string //Truncate from the second subscript to the total length + 1, so the result output is: 2,3 2. substr()The method can extract the characters from the subscript to the specified number of characters in the string. var str="Hello!"; var n=str.substr(2,3) // Extract 3 characters from the character with subscript 2, and the output result is: llo 3.indexOf()The method returns the position of the first occurrence of a specified string value in a string, or -1 if no match is found. var str="runab site"; var n=str.indexOf("a"); //If only one parameter is specified, the query will start from the beginning, specify the string value to be searched, and return the specified index after the query //The second parameter specifies the position to start searching in the string (including the input index), and returns the specified index after the query //The output result is: 3 4.lastIndexOf()The method returns the position of the last occurrence of a specified string value, or -1 if no matching string is found. var str="runab site"; var n=str.lastIndexOf("a",3); //If only one parameter is specified, the query will start from the end and go forward. The specified index will be returned after the query. //The second parameter is the character with the subscript number to query forward (including the input subscript). The specified index will be returned after the query. //The output result is: 3 //Combined with the above (return the last character) let str = str.substring(0, str.lastIndexOf('e')); 5.replace()The method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression. var str="Hello everyone!"; var n=str.replace("big","small"); //The output result is: Hello, little family! //You can also use regular expression to replace var str="everyone is so big"; var n=str.replace(/大/g,"小"); //g is global replacement //output result is: 小家好小 //set the second parameter to empty to delete the string var str="大家好大"; var n=str.replace(/大/g,""); //g is a global replacement //The output result is: 家好 Sometimes the characters need to be spliced after being intercepted, which can be done like this var str="hello world!" var items = str.split("ll") // ["he", "oWorld!"] //You will get an array, the items array includes multiple strings separated by ll (excluding ll) var newStr=items.join(""); // heoWorld! //join() uses an empty string to connect the arrays in the array into a new string. Without quotes, commas are used as separators by default. Replenish:
This concludes this article about how to use You may also be interested in:
|
<<: Completely delete MySQL steps
>>: Detailed explanation of homology and cross-domain required for front-end interviews
Preface Learn to create a file system on your sys...
Table of contents 1. Limit props to type lists 2....
Table of contents Variable Scope The concept of c...
Download and installConfigure environment variabl...
Table of contents Introduction effect principle f...
Preface In our daily development process, sorting...
Table of contents Method 1: The simplest way to s...
A common development need is that we want to coll...
Table of contents Avoid repetitive rendering loop...
Table of contents 1. Preparation Pull the redis i...
The META tag, commonly referred to as the tag, is...
Abstract: MySQL provides a variety of storage eng...
Installing Electron cnpm install electron -g Inst...
The specific method of installing CentOS 7.0 on V...
Preface We all know that startups initially use m...