String properties and methods Flashcards
How do we get the length of a string?
syntax, parameters and expected results?
Use case?
String.length
returns the length of a string, only works on strings. Used when we want the length of string duh.
What does the method to get a value at a certain index?
syntax, parameters and expected results?
Use case?
Return the character or character code at the specified position in string. string.charAt(0);
returns the character at that index of a string, as opposed to indexOf returning the index.
What does the str.indexOf() or lastIndexOf() method do?
syntax, parameters and expected results?
Use case?
Return the position of specified substring in the string or last position of specified substring, respectively.
Syntax as is, and used to find the index of a char
What does the str.includes() str? syntax?
syntax, parameters and expected results?
Use case?
Returns whether or not the string starts, ends or contains a specified string.
What does the str.concat() method do? syntax?
syntax, parameters and expected results?
Use case?
Combines the text of two strings and returns a new string. string.concat(str,…str)
Used to combine two strings, result is a new string, can take a spread operator
What does the str.split() method do? syntax?
syntax, parameters and expected results?
Use case?
string.split(/regexp/,splitLimit)
Splits a String object into an array of strings by separating the string into substrings.
used to split a string into an array, you can pass a limit as a second variable to limit how many splits to make in the array leftover values are not included in the new result.
What does the str.slice() method do?
syntax, parameters and expected results?
Use case?
Extracts a section of a string and returns a new string.
str.slice(beginIndexIncluded,endIndexExcluded)
used when we want to retrieve part of a string.
How do we repeat a string literal?
syntax, parameters and expected results?
Use case?
str.repeat(n); or This is a ${name}
.repeat(n);
Returns a string consisting of the elements of the object repeated the given times.
Used when we want to repeat stuff!
How to capitalize a string and or make it all lower case, or just the first letter?
syntax, parameters and expected results?
Use case?
str.toLowerCase()
str[0].toUpperCase()+str.slice(1);
what will str=’test’; return test[0] return?
What will return ‘test2’[0] result in?
They both will just return the first character of the string.
What is the object & methods used to turn a variable into a string?
syntax, parameters and expected results?
Use case?
String(string) and var.toString();
Will return a new value as a string.
What are the properties methods that we can use on both arrays and strings?
syntax, parameters and expected results?
Use case?
includes(string) slice(firstindex(included),lastindex(excluded)) indexOf() lastIndexOf() .length .concat();
What are the methods we can use on strings that can take regexp?
syntax, parameters and expected results for each?
Use case for each?
string.search(/regexp/)
returns the index of regexp match or -1
string.match(/regexp/),
returns array of matches or null
string.replace(/regexp/ORsubstr, newsubstr OR function) important refer to docs for replacement parameters
returns a string with all all or some replacements
/regexp/ORstr.test(substring)
returns true or false depending on if the string is found in the Regexp can be powerful if used in place of if statements for matches!!
string.split(/regexp/)
returns a new split array
What is the method to retrieve an array of results that pass a regex or string test?
syntax, parameters and expected results?
Use case?
string.split(/regexp/)
Splits a String object into an array of strings by separating the string into substrings.
used to split a string into an array, you can pass a limit as a second variable to limit how many splits to make in the array leftover values are not included in the new result.
What is the method used to change a specific value in a string?
syntax, parameters and expected results?
Use case?
string.replace(/regexp/,replacement)
Use to replace 1 or all matches in string, returns a new string!