Learn the basics is to learn a language foundation, the following string, together to learn.
Unlike string and string, string is a constructor, and string is a type of variable. (String is an instance of string)
Use after declaring a string, var str = "Wo shi yi ge hao xue sheng"
is included
① determines whether a string is contained in another string, contains return True, does not contain a return of false Str.includes (Searchstr,formindex) Searchstr the string that the query contains Formindex where to start.
Str.includes (' wo ')//true
Cutting string
① gets a section of the string, returns a new string Str.slice (Start,end) If it is negative, it is treated as start+str.length, end.
② the string as an array, returning an str.split (separator, howmany) separator can be a string or regular expression, Howmany is the length of the array after the cut
Str.split (/i/g)//["Wo sh", "y", "ge Hao Xue Sheng"]
③ gets a certain number of characters from the beginning of the string subscript, returns a new string Str.substr (start,length) Start can be negative, if negative is the inverse index
④ Extracts character str.substring (start,end) start,end nonnegative integers between two specified subscripts
Find the location of the string |
① find out if a character exists, where it returns the first occurrence, no return -1,fromindex is an optional option, and, if so, is checked from an index position str.indexof (SEARCHSTR,FROMINDEX)
Str.indexof (' Shi ')//return first occurrence position, 3
str.indexof (' Shi ', 8)//Return-1
② returns the last occurrence of the specified string, searching backwards, fromindex as the starting index Str.lastindexof (searchstr,fromindex)
Str.lastindexof (' Shi ')//return to the last occurrence position, 3
str.lastindexof (' Shi ', 2)//return-1
③ query the matching of regular expressions within a string, returns an array, cannot find a return null, and if the argument is not regular, it is forcibly converted to a regular str.match (REGEXP)
Str.match (/[1-9]/g)//null
str.match (/\d/g)//["W", "O", "s", "H", "I", "Y", "I", "G", "E", "H", "A", "O", "X", "U", " E "," s "," H "," E "," N "," G "]
④ returns the index of the first character of the substring that matches the regular expression, and does not return-1 str.search (REGEXP)
About the encoding of
① returns the encoding of a location Str.charcodeat (index)
Str.charcodeat (1)//The code that returns the position of index 1, 111
② creates a string with some encoding String.fromCharCode ()
String.fromCharCode (111)//compiled string is O
About the location of
① returns the character of a position Str.charat (index)
Str.charat (1)//Returns the character of the position indexed to 1, O
Replace text
①. Replace the text that matches the regular expression str.replace (REGEXP,STR)
Str.replace (/hao/g, ' Huai ')//wo shi Yi ge huai xue
②. Remove the space from the two paragraphs of the string and return the new string Str.trim ()
and write Concat () connection string, I can think of the only advantage is that I write a few + number connectors, said Concat () thought of repeat () how to do ... str.repeat (num)
I hope this article will help you with JavaScript programming.