Learning a good foundation is the foundation of learning a language well. Let’s organize String and encourage them to learn together.
String is different from string. String is a constructor and string is a type of variable. (string is an instance of String)
Use it after declaring a string, var str = "wo shi yi ge hao xue sheng"
Whether it contains
① Determine whether a string is contained in another string, including return true, and does not include return false (searchStr,formIndex). SearchStr The query starts from where the string formIndex included.
('wo') //true
Cut string
① Get a certain segment of the string and return a new string (start, end). If start is a negative number, it will be treated as start+, the same as end.
(-2) //ng
② Cut the string into an array and return an array (separator, howmany) separator can be a string or a regular expression, howmany is the length of the cut array
(/i/g) //["wo sh", " y", " ge hao xue sheng"]
③ Get a certain number of characters from the beginning of the string subscript and return a new string (start, length) start can be a negative number. If it is a negative number, it is the index of the reciprocal number.
(-2) //ng
④ Extract characters (start, end) start, end non-negative integers in the string
(1,2) //o
Find the location of the string | Whether it exists
① Find whether a character exists. It will return to the location where it first appears. If it does not exist, it will return -1. FromIndex is an optional option. If it does, it will start to check from a certain index position (searchStr, fromIndex)
('shi') //Return to the first appearance position, 3 ('shi',8) //return-1
② Return the last location of the specified string, search from behind and forward, fromIndex is the index of the beginning (searchStr, fromIndex)
('shi') //Return to the last occurrence position, 3('shi',2) //Return -1
③ Query the match of regular expressions in a string, return an array, and return null cannot be found. If the parameter is not a regular, it will be forced to convert to regular (RegExp)
(/[1-9]/g) //null (/\d/g) //["w", "o", "s", "h", "i", "y", "i", "g", "e", "h", "a", "o", "x", "u", "e", "s", "h", "e", "n", "g"]
④ Return the index of the first character of the substring matching the regular expression, without returning -1 (RegExp)
(/wo/g); //0
About encoding
① Return to the encoding of a certain location (index)
(1) // Return the encoding of the position with index 1, 111
②Create a string through some encoding ()
(111) //The compiled string is o
Related location
① Return the character at a certain position (index)
(1) // Return the character with index 1, o
Replace text
①.Replace text matching regular expressions (RegExp, str)
(/hao/g,'huai') //wo shi yi ge huai xue sheng
②. Remove the spaces in the two segments of the string and return the new string ()
There are also those who write concat() to connect strings. The only benefit I can think of is that I write less + sign connectors. When it comes to concat(), I think of what to do if repeat() is... (num)
I hope this article will be helpful to everyone's JavaScript programming.