String Methods Flashcards
Property that returns the length of a string
String.length
Returns the index of the first occurrence of a specified string in a string
indexOf()
Returns the index of the last occurrence of a specified text in a string by searching backwards
lastIndexOf()
indexOf() and lastIndexOf() return what if the text is not found?
-1
How to add a starting position for indexOf()/lastIndexOf()
indexOf(string, 15)
Searches for a string with a specified value and returns the position to the match
search()
What is the difference between search() and indexOf()?
search() cannot take a second start position argument.
indexOf() cannot take powerful search values/regular expressions
Extracts the part of a string and returns the extracted part in a new string with the second parameter specifying the end index.
slice(start, end*)
*end can be ommited to slice out the rest of the string. a negative start can be used to count from the end.
Extract the part of a string and returns the extracted part in a new string but cannot take a negative index.
substring(start, end)
Extract the part of a string and returns the extracted part in a new strung with the second parameter specifying length
substr(start, length)
a negative start can be used to count from the end.
Replaces the specified value with another value in a string
replace()
*To replace case insensitive, use a regular expression with an /i flag (insensitive)
*
To replace all matches, use a regular expression with a /g flag (global match):
Convert a string to uppercase
toUpperCase()
Convert a string to lowercase
toLowerCase()
Joins two or more strings. works like a plus operator
concat()
Remove white space from both sides of a string
trim()
*not supported by internet Explorer 8. Use replace() with a regular expression instead.
Returns the character at a specified index in a string
CharAt()
Returns the unicode of the character at a specified index in a string (in UTF-16 code, an int between 0 and 65535)
CharCodeAt()
How does property access work in a string?
- It makes strings look like arrays (but they are not)
- If no character is found, [ ] returns undefined
- It is read only. str[0] = “A” gives no error (but does not work!)
Convert a string to an array
split(seperator*)
*takes in a string as separator. if omitted, returns an array with the whole string in index[0]