Strings Flashcards

1
Q

to check if the string starts with some character(s), return boolean

A

string.startsWith()
var foo = “banana”
foo.startsWith(“b”) // true
foo.startsWith(“z”) // false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

to check if the string contains a specific character(s), return boolean

A

string.includes()
var foo = “banana”
foo.includes(“z”) // false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

to make a string into uppercase. Is not modifying original string

A

string.toUpperCase()
var foo = “BanAnA”
var newFoo = foo.toUpperCase()
console.log(newFoo) // “BANANA”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

to make a sting into lowercase. Is not modifying original string

A

string.toLowerCase()
var foo = “BanAnA”
var newFoo = foo.toLowerCase()
console.log(newFoo) // “banana”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

to get back a character at specific position/index. Does not modify the string.

A

string.charAt()
var foo = “banana”
foo.charAt(0) // b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

To get back the character(s) from a string starting from a specific index. Returns a substring

A

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”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To replace the first instance of a given character(s) with the value of the second argument, is not modifying the initial string

A

string.replace()
string.replace(pattern, replacement);
var foo = “banana”
foo.replace(“a”, “z”) // “bznana”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

To replace all instances of a given character(s) with the value of the second argument. Is not modyfing the initial string.

A

string.replaceAll()
string.replaceAll(pattern, replacement);
var foo = “banana”
foo.replace(“an”, “zz”) // “bzzzza”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To repeat a string a given number of times

A

string.repeat()
var foo = “banana”
foo.repeat(2) // “bananabanana”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

This method splits the string based on the provided argument. (5 uses!)

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Transforms an array into a string with a provided argument. Argument is used to define the joining element.

3

A

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”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly