Strings Flashcards
to check if the string starts with some character(s), return boolean
string.startsWith()
var foo = “banana”
foo.startsWith(“b”) // true
foo.startsWith(“z”) // false
to check if the string contains a specific character(s), return boolean
string.includes()
var foo = “banana”
foo.includes(“z”) // false
to make a string into uppercase. Is not modifying original string
string.toUpperCase()
var foo = “BanAnA”
var newFoo = foo.toUpperCase()
console.log(newFoo) // “BANANA”
to make a sting into lowercase. Is not modifying original string
string.toLowerCase()
var foo = “BanAnA”
var newFoo = foo.toLowerCase()
console.log(newFoo) // “banana”
to get back a character at specific position/index. Does not modify the string.
string.charAt()
var foo = “banana”
foo.charAt(0) // b
To get back the character(s) from a string starting from a specific index. Returns a substring
string.slice()
takes 1 or 2 arguments, with one argument returns the characters starting from the provided index. With 2 arguments returns characters starting from the first argument/index till second argument/index. Is not modifying the original
var foo = “banana” foo.slice(1) // “anana” foo.slice(1, 3) // “an”
To replace the first instance of a given character(s) with the value of the second argument, is not modifying the initial string
string.replace()
string.replace(pattern, replacement);
var foo = “banana”
foo.replace(“a”, “z”) // “bznana”
To replace all instances of a given character(s) with the value of the second argument. Is not modyfing the initial string.
string.replaceAll()
string.replaceAll(pattern, replacement);
var foo = “banana”
foo.replace(“an”, “zz”) // “bzzzza”
To repeat a string a given number of times
string.repeat()
var foo = “banana”
foo.repeat(2) // “bananabanana”
This method splits the string based on the provided argument. (5 uses!)
string.split(separator, limit);
limit restricts the number of splits performed
Returns an array of substrings
with Separator var str = “appel,banana,cherry” var result = str.split(“,”) // [“apple”,”banana”,”cherry”] no Separator (returns entire) var str = “appel,banana,cherry” var result = str.split() // [“apple,banana,cherry”]
Empty Seperator (Splitting every character)
var str = “hello”;
var result = str.split(“”); // [“h”, “e”, “l”, “l”, “o”]
Splitting with Limit var str = “appel,banana,cherry” var result = str.split(“,” , 2) // [“apple” , “banana “] using a regular expression const str = "apple banana cherry"; const result = str.split(/\s+/); // Split by one or more spaces console.log(result); // ["apple", "banana", "cherry"]
Transforms an array into a string with a provided argument. Argument is used to define the joining element.
3
array.join()
array.join(separator)
var fruits = [“apple”,”banana”,”cherry”];
fruits.join(“, “) // “apple, banana, cherry”
returns a new string default seperator is comma, but any string is possible. If array is empty returns empty string If string is empty(“”) all elements are joined without any characters If array contains non-string elements, they will be converted let mixed = [1, true, null, “hello”] mixed.join(“ | “) // “1 | true | null | hello”