Strings : Methods and Properties Flashcards
string.split()
Separates the characters or words within a string into an array. Optional separator parameter (“” or “ “) separates by each character or by word, respectively. No separator returns the entire string as one element in the array
string.replace(substr | regexp, new SubStr | function())
Returns a new string with some or all matches of a pattern of characters replaced by a new pattern. The pattern can be a string or a regexp, and the replacement can be a string or a function to be called for each match.
string.length
This property returns the number of code units in the string. UTF-16, the string format used by JavaScript, uses a single 16-bit code unit to represent the most common characters, but needs to use two code units for less commonly-used characters, so it’s possible for the value returned by length to not match the actual number of characters in the string.
For an empty string, length is 0.
string.substr()
The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.
syntax: str.substr(start[, length])
Parameters:
start
Location at which to begin extracting characters. If a negative number is given, it is treated as strLength + start where strLength is the length of the string (for example, if start is -3 it is treated as strLength - 3.)
length
Optional. The number of characters to extract.
string.slice()
The slice() method extracts a section of a string and returns a new string.
Syntax: str.slice(beginSlice[, endSlice])
Parameters:
beginSlice
The zero-based index at which to begin extraction. If negative, it is treated as sourceLength + beginSlice where sourceLength is the length of the string (for example, if beginSlice is -3 it is treated as sourceLength - 3).
endSlice
Optional. The zero-based index at which to end extraction. If omitted, slice() extracts to the end of the string. If negative, it is treated as sourceLength + endSlice where sourceLength is the length of the string (for example, if endSlice is -3 it is treated as sourceLength - 3).