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
background An nginx server module needs to proxy ...
CSS 3 animation example - dynamic effect of Tab b...
Table of contents 1. Transition from development ...
In the previous article https://www.jb51.net/arti...
HTML tags explained 1. HTML tags Tag: !DOCTYPE De...
I have been using CSS for a long time, but I have...
Table of contents 1. Control the display and hidi...
1. Download Dependency npm install @antv/data-set...
Preface I am currently working on a high-quality ...
I suddenly thought of this method when I was writi...
introduction Looking back four years ago, when I ...
Sometimes the theme of a project cannot satisfy e...
Overview: I drew lessons from several timetable s...
Table of contents 1. First, use pycharm to create...
Preface After a failover occurs, a common problem...