SoFunction
Updated on 2025-04-10

Summary of the commonly used string processing methods of JS

() method, find the string position from front to back, case sensitive, and count from 0. Similarly, the lastIndexOf() method is from the back to the front, and the results of the two methods output the same search conditions are the same.

For example:
Copy the codeThe code is as follows:

<script type="text/javascript">

var str="Hello World!"
(("Hello"))//Output 0
(("World"))//Output 6
(("world"))//Output -1, because no searches were found

</script>

, accessed in the form of "" because it is a method of string object
Copy the codeThe code is as follows:

<script type="text/javascript">

var str="Hello World!"
();//Output 12

</script>

() method, used for string interception, a required parameter, an optional parameter, counting starting from 0
Copy the codeThe code is as follows:

<script type="text/javascript">

var str="Hello World!"
((3));//Output lo World!, starting with characters with ordinal number 3 (including characters with ordinal number 3), and when there is only one parameter, it will be output to the end
((3,7));//Output lo Worl, if the first parameter is a negative number, it is a reverse number

</script>

() method, used to return characters at the specified position, counting starting from 0
Copy the codeThe code is as follows:

<script type="text/javascript">

var str="Hello World!"
((1));//Output e

</script>

() method, used to split a string into a string array
Copy the codeThe code is as follows:

<script type="text/javascript">

var str="Hello World!"
((" "));//Output Hello,World!
((""));//Output H,e,l,l,o,W,o,r,l,d,!
((" ",1));//Output Hello
"2:3:4:5".split(":")//It will return ["2", "3", "4", "5"]
"|a|b|c".split("|")//It will return ["", "a", "b", "c"]
var words = (/\s+/)//Use regular expressions as split parameters

</script>