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
What does
String.prototype.startsWith()
do?
Returns true if a string begins with the argument characters
What does
String.prototype.endsWith()
do?
Returns true if a string ends with the argument characters
Return true if a string begins with the argument characters
String.prototype.startsWith()
Return true if a string ends with the argument characters
String.prototype.endsWith()
What does
String.prototype.substr()
do?
Returns a section of a string (using first index and number of characters rather than last index)
The first argument may be negative, to count from the end:
alert( str.substr(-4, 2) ); // ‘gi’, from the 4th position get 2 characters
Return a section of a string (using first index and number of characters rather than last index. also negative indexes will count from the end. The last index being -1)
String.prototype.substr()
Drawback of String.prototype.substr()
Formally, substr has a minor drawback: it is described not in the core JavaScript specification, but in Annex B, which covers browser-only features that exist mainly for historical reasons. So, non-browser environments may fail to support it. But in practice it works everywhere.
Compare 2 strings for alphabetical order rather than unicode numerical order
Returns a negative number if str1 is less than str2.
Returns a positive number if str1 is greater than str2.
Returns 0 if they are equivalent.
String.prototype.localeCompare()
const a = 'réservé'; // with accents, lowercase const b = 'RESERVE'; // no accents, uppercase
[[if > is true return 1. If < is true return -1. If equal return 0.]]
console.log(a.localeCompare(b)); // expected output: 1 console.log(a.localeCompare(b, 'en', { sensitivity: 'base' })); // expected output: 0
What does
String.prototype.localeCompare()
do and/or return?
Compare 2 strings for alphabetical order rather than unicode numerical order
Returns a negative number if str is less than str2.
Returns a positive number if str is greater than str2.
Returns 0 if they are equivalent.
[[if > is true return 1. If < is true return -1. If equal return 0.]]
const a = 'réservé'; // with accents, lowercase const b = 'RESERVE'; // no accents, uppercase
console.log(a.localeCompare(b)); // expected output: 1 console.log(a.localeCompare(b, 'en', { sensitivity: 'base' })); // expected output: 0
What’s the similaritiesdifference between
String.prototype.slice()
String.prototype.substr()
String.prototype.substring()
in terms of:
- Start End
- Purpose
- Mutation
- Second argument smaller
- first argument requirement
- second argument requirement
Start End
string. slice(start,end)
string. substr(start,length)
string. substring(start,end)
Purpose The slice() method extracts parts of a string and returns the extracted parts in a new string. The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. The substring() method extracts parts of a string and returns the extracted parts in a new string.
Mutation
slice() Doesn’t
substr() Doesn’t
substring() Doesn’t
Negative Numbers
slice() selects characters starting from the end of the string
substr()selects characters starting from the end of the string
substring() Doesn’t Perform (just treats negatives as 0)
Second argument smaller
slice() Doesn’t Perform (returns an empty string)
substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems
substring() will swap the two arguments, and perform as usual
First argument requirement
slice() Required, indicates: Starting Index
substr() Required, indicates: Starting Index
substring() Required, indicates: Starting Index
Second argument requirement
slice() Optional, The position (up to, but not including) where to end the extraction
substr() Optional, The number of characters to extract
substring() Optional, The position (up to, but not including) where to end the extraction
IF OMITTED
slice() selects all characters from the start-position to the end of the string
substr() selects all characters from the start-position to the end of the string
substring() selects all characters from the start-position to the end of the string