Strings Flashcards

1
Q

alert( My\n.length ); // returns and why?

A

3

Note that \n is a single “special” character, so the length is indeed 3.

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

let str = Hello;

alert( str[-2] ); //

A

undefined

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

let str = Hello;

alert( str[0] ); // returns?

A

H

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

let str = Hello;

// the first character
alert( CODE HERE ); // H
alert( CODE HERE ); // H

A

str[0]
str.at(0)

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

let str = Hello;

// the last character
alert( ); // o
alert( );

A

str[str.length - 1]
str.at(-1)

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

how to iterate through “hello”
use for..of

A

for (let char of “Hello”) {
alert(char);
}

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

let str = ‘Hi’;
str[0] = ‘h’; // error
alert( str[0] ); // doesn’t work

how to fix

A

str = ‘h’ + str[1]; // replace the string

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

change to upper or lower case

A

.toUpperCase()
.toLowerCase()

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

It looks for the substr in str, starting from the given position pos, and returns the position where the match was found or -1 if nothing can be found.

A

str.indexOf()

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

let str = ‘Widget with id’;

alert( str.indexOf(‘widget’) ); //

A

-1, not found, the search is case-sensitive

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

let str = ‘Widget with id’;

alert( str.indexOf(‘id’, 2) ) //

A

12

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

alert(‘interface’ ); //
change first i to lower case

A

‘Interface’[0].toLowerCase()

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

let str = “Widget with id”;

if (str.indexOf(“Widget”)) {
alert(“We found it”); // doesn’t work!
}

how to fix?

A

let str = “Widget with id”;

if (str.indexOf(“Widget”) != -1) {
alert(“We found it”); // works now!
}

The alert in the example above doesn’t show because str.indexOf(“Widget”) returns 0 (meaning that it found the match at the starting position). Right, but if considers 0 to be false.
So, we should actually check for -1, like this:

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

The more modern method ________returns true/false depending on whether str contains substr within.

A

str.includes(substr, pos)

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

alert( “Widget” );
// true, “Widget” starts with “Wid”

alert( “Widget” );
// true, “Widget” ends with “get”

A

.startsWith(“Wid”)

.endsWith(“get”)

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

There are 3 methods in JavaScript to get a substring:

A

substring, substr and slice.

17
Q

Returns the part of the string between start and end (not including end). This is almost the same as slice, but it allows start to be greater than end (in this case it simply swaps start and end values).

A

str.substring(start [, end])

18
Q

let str = “stringify”;
alert( str.slice(0, 5) ); //returns

A

‘strin’, the substring from 0 to 5 (not including 5)

19
Q

let str = “stringify”;
alert( str.slice(0, 1) ) // returns

A

; // ‘s’, from 0 to 1, but not including 1, so only character at 0

20
Q

let str = “stringify”;
alert( str.slice(2) ); //

A

‘ringify’, from the 2nd position till the end
If there is no second argument, then slice goes till the end of the string:

21
Q

let str = “stringify”;
alert( str.slice(2, 6) );

A

// “ring”

22
Q

Returns the part of the string from start, with the given length

A

str.substr(start [, length])