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)