1. The charAt() method of string
'string.chartat (index)' charAt() returns the specified character from a string.Copy the code
-
Index (0 ~ string. length-1) indicates the length between 0 and -1
-
Search order from left to right, the first character index is 0, the last character index is length-1
-
If index is out of range, an empty string is returned
For me, charAt() and indexOf() are very hard to tell apart, although it’s easy to tell apart by the literal meaning of the word, char at characters in… IndexOf (), but the actual use of indexOf() is often forgotten. Well, let’s, uh, watch it again.
2. String indexOf() method
String.indexof (searchValue [, fromIndex]) indexOf() returns the indexOf the first occurrence of the specified value in the string that called it, starting with fromIndex and returning -1 if not found
SearchValue is the character to look for, default is “undefined” if no value is set, and then look for “undefined” in the string
FromIndex Optional value. Any integer. Default is 0, indicating where to start.
If the searchValue is empty, see the MDN document String.indexof ().
Check whether the String contains a character string.indexof (value)! == -1 and the indexOf() method is case sensitive
3. String includes()
Str.includes (searchString[, position]) is used to determine whether a string is included in another string, returning true and false
if (! String.prototype.includes) { String.prototype.includes = function(search, start) { 'use strict'; if (typeof start ! == 'number') { start = 0; } if (start + search.length > this.length) { return false; } else { return this.indexOf(search, start) ! = = 1; }}; }Copy the code
The string.indexof () method obtains the location, and the string.includes() method obtains the existence.
3. string.split()
String.split () splits a string into an array of substrings using the delimiter string
4. string.slice()
The slice(beginIndex[, endIndex]) method extracts a portion of a string and returns a new string, leaving the original string unchanged.
- BeginIndex extracts characters from the original string starting at the index (base 0). If the value is negative, it will be treated as strLength + beginIndex
- EndIndex extracts characters from the original string at the end of the index (base 0). If the value is negative, it is treated as strLength + endIndex
- The new string extracted by slice() includes beginIndex but not endIndex