Strings - methods Flashcards

1
Q

length:

A

let str = “Hello, world!”;
console.log(str.length); // outputs 13

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

charAt(index):

A

let str = “Hello, world!”;
console.log(str.charAt(0)); // outputs “H”

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

substring(start, end):

A

let str = “Hello, world!”;
console.log(str.substring(0, 5)); // outputs “Hello”

he start argument is the index of the first character to include in the returned string, and the end argument is the index of the first character to exclude

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

substr(start, length):

A

let str = “Hello, world!”;
console.log(str.substr(7, 5)); // outputs “world”

The substr(start, length) method returns a part of the string starting at the specified start index (position) and extending for the specified number of characters.

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

slice(start, end):

A

let str = “Hello, world!”;
console.log(str.slice(7, 12)); // outputs “world”

The start argument is the index of the first character to include in the returned string, and the end argument is the index of the first character to exclude.

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

indexOf(searchValue, fromIndex):

A

let str = “Hello, world!”;
console.log(str.indexOf(“world”)); // outputs 7

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

lastIndexOf(searchValue, fromIndex):

A

let str = “Hello, world! world”;
console.log(str.lastIndexOf(“world”)); // outputs 13

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

includes(searchString, position):

A

let str = “Hello, world!”;
console.log(str.includes(“world”)); // outputs true

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

replace(searchValue, replaceValue):

A

let str = “Hello, world!”;
console.log(str.replace(“world”, “friend”)); // outputs “Hello, friend!”

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

split(separator, limit):

A

let str = “Hello, world!”;
console.log(str.split(“, “)); // outputs [“Hello”, “world!”]

The split(separator, limit) method splits a string into an array of substrings and returns the array. The separator argument is the string to use as the separator between each element in the returned array, and the optional limit argument is the maximum number of elements to include in the returned array.

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

regex methods (match, search, replace):

A

let str = “Hello, world!”;
console.log(str.search(/world/)); // outputs 7

let str = “Hello, world!”;
console.log(str.replace([/world/], “friend”)); // outputs “Hello, friend!”

let str = “Hello, world!”;
let result = str.match(/Hello, (\w+)/);
console.log(result); // outputs [“Hello, world”, “world”]

The match() method matches the string str against the regular expression /Hello, (\w+)/. The (\w+) capture group matches the word “world” in the string, and the result of the match is stored in an array result. The first element of the array result[0] is the entire matched string “Hello, world”, and the second element result[1] is the capture group “world”.

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