JS Strings Flashcards
.charAt(index)
str.charAt(0);
returns character at specified index
.charCodeAt(index)
str.charCodeAt(0);
returns unicode of character at specified index
.concat(str, ?…)
str1.concat(str2, str3, …);
joins 2+ strings and returns one string
.endsWith(searchvalue, ?length)
default length is string length
str.endsWith(“bye.”);
returns boolean, checks whether string ends with specified characters (case-sensitive)
String.fromCharCode(char_code,…)
String.fromCharCode(72, 69, 76, 76, 79);
converts a unicode number to a character
.includes(searchvalue, ?startindex)
str.includes(“world”);
returns boolean whether string includes specified string
.indexOf(searchvalue, ?start)
str.indexOf(“world”);
returns index of first occurrence of search value in string, returns -1 if no value found
.lastIndexOf(searchvalue, ?start)
str.lastIndexOf(“world”);
returns index of last occurrence of search value in string, returns -1 if no value found
.localCompare(comparestring)
str1.localCompare(str2);
compares two string in the current locale, returns -1,0,1 depending on sort order comparing; -1 reference sorts before compare string…
.match(regex)
str.match(/rain/i);
returns an array of values matching the regex
.repeat(number)
str.repeat(2)
returns a new string with set number of duplicates of reference string
.replace(searchvalue,newvalue)
str.replace(/blue/g, “red”);
returns new string replacing search value (string or regex) with new value
.search(searchvalue)
str.search(“world”);
returns a Number, representing the position of the first occurrence of the specified searchvalue, or -1 if no match is found
string.slice(start, ?end)
str. slice(1, 5); (incl 1, excl 5)
str. slice(0) extracts whole string
str. slice(-1) extracts last character
returns a String, representing the extracted part of the string.
string.split(separator, ?limit)
str.split(“ “);
str.split(“”) splits every value
limit cuts off array after set number of items
returns an Array, containing the splitted values