String Methods Flashcards
What does String.length do?
Returns the length of a string (technically property)
What does String.slice(start, end) do?
Extracts a part of a string (specify where it starts and ends in the parameters) and returns it in a new string.
What does String.substring(start, end) do?
Extracts part of a string and returns it as a new string.
What is the difference between substring and splice?
If start > stop, then substring will swap those 2 arguments. With substring, if it is NaN or negative it will treat the value as 0.
What does String.substr(start, end) do?
Extracts a part of a string and returns a new string - the second parameter specifies the length of the extracted part (that is what makes it different)
What does String.replace(thing you want to replace, replace with)
Replaces a specified value with another value in a string - returns a new string
What are some limitations of String.replace()
Only returns the first match (use /g flag to replace all), does not change the string it is called on, case sensitive, (use /i to ignore the case)
What does String.replaceAll() do?
Allows you to specify a regular expression instead of a string to be replaced - if the parameter is a regular expression it