String Methods Flashcards
Return string with spaces removed from beginning and end from an old string
Return a string with spaces removed from the beginning (2 ways)
Return a string with spaces removed from the end
myString.trim()
myString.trimStart() or trimLeft()
myString.trimEnd() or trimRight()
Return string all upper case or lower case from an old string
myString.toUpperCase()
myString.toLowerCase()
(3 methods)
Parse string for something, return boolean if it’s there, 2 ways
- How do they each respond to regex?
- Can they search parts of arrays?
There’s a third way that returns an array of what it finds and/or info about what it finds.
/thingsToTestFor/.test(string or array to search in)
- HAS to use regex
- Can search parts of elements in arrays.
- searches within all levels of nested arrays
Regex String
myStringOrArray.includes()
- CAN’T use regex
- Can’t search for parts of elements in arrays
String String
myString.match( )
- It can use regex
- returns either the element or an array with all instances of the element with the global tag
String Regex
Parse strings for something and return an array of:
The matched string, the Index# where the substring starts, the original string, groups
myString.match()
With g flag it returns an array with all instances of the find.
Split a string into an array. Splitsat the argumentstring characters
myString.split(‘ ‘, number of items to split)
Find a substring within a string. Replace it with something. Return the new string.
How does it respond to regex?
myString.replace(thingToFind, thingToReplace)
Can use / / g with thingToFind to replace all instances
Return part of a string based on index positions of characters (2 ways)
myString.substring(startIndex, endIndexExclusive)
myString.slice()
Concatenate a string 2 ways
‘string 1’ + ‘ string 2’
‘string 1’.concat(‘ string 2’, ‘string3’)
What is logged from:
let apply = ‘012345’;
console.log(apply.substring(4,1));
‘123’
substring should be (start index inc, end index exc)
It swaps the arguments in .substring() when the start index > end index
What does
String.prototype.trim()
do?
and/or return?
The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
trim()
What does String.prototype.trimStart() String.prototype.trimLeft() do? and/or return?
The trimStart() method removes whitespace from the beginning of a string. trimLeft() is an alias of this method.
trimStart()
trimLeft()
What does
String.prototype.trimEnd()
String.prototype.trimRight()
do?
The trimEnd() method removes whitespace from the end of a string. trimRight() is an alias of this method.
trimEnd()
trimRight()
What does
String.prototype.toUpperCase()
do?
The toUpperCase() method returns the calling string value converted to uppercase (the value will be converted to a string if it isn’t one).
toUpperCase()
What does
String.prototype.toLowerCase()
do?
and/or return?
The toLowerCase() method returns the calling string value converted to lower case.
toLowerCase()
What does
RegExpObject.test()
do?
and/or return?
The test() method executes a search for a match between a regular expression and a specified string. Returns true or false. Regex String
test(str)
What does
String.prototype.includes()
do?
and/or return?
The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate. String String
includes(searchString)
includes(searchString, position)
(There is also an Array.prototype.includes())
What does
String.prototype.match()
do?
and/or return?
The match() method retrieves the result of matching a string against a regular expression. String Regex
Returns an array of info:
[ ‘a’, index: 0, input: ‘apple’, groups: undefined ]
with g flag it returns an array of all found instances
match(regexp)
What does
String.prototype.split()
do?
and/or return?
The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.
split()
split(separator)
split(separator, limit#ofSplits)
What does
String.prototype.replace()
do?
and/or return?
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. 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. If pattern is a string, only the first occurrence will be replaced.
The original string is left unchanged.
replace(regexp, newSubstr)
replace(regexp, replacerFunction)
replace(substr, newSubstr)
replace(substr, replacerFunction)
What does
String.prototype.substring()
do?
and/or return?
The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
substring(indexStart)
substring(indexStart, indexEnd)
What does
String.prototype.concat()
do?
and/or return?
The concat() method concatenates the string arguments to the calling string and returns a new string.
concat(str1)
concat(str1, str2)
concat(str1, str2, … , strN)
(There is also an Array.prototype.concat())
What does
String.prototype.charAt()
do?
and/or return?
The String object’s charAt() method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string.
charAt(index)
What’s the difference between:
String.prototype.includes()
String.prototype.match()
RegExpObject.prototype.test()
.includes() - The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.
String String
.match() - returns an array of strings against a regular expression
String Regex
RegExpObject.test() - returns boolean if a string argument is in the the regular expression
Regex String
What does
String.prototype.indexOf()
do?
and/or return?
The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
indexOf(searchValue)
indexOf(searchValue, fromIndex)
let str = ‘Widget with id’;
alert( str.indexOf(‘Widget’) ); // 0, because ‘Widget’ is found at the beginning
alert( str.indexOf(‘widget’) ); // -1, not found, the search is case-sensitive