String properties and methods Flashcards

1
Q

How do we get the length of a string?

syntax, parameters and expected results?
Use case?

A

String.length

returns the length of a string, only works on strings. Used when we want the length of string duh.

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

What does the method to get a value at a certain index?

syntax, parameters and expected results?
Use case?

A

Return the character or character code at the specified position in string. string.charAt(0);

returns the character at that index of a string, as opposed to indexOf returning the index.

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

What does the str.indexOf() or lastIndexOf() method do?

syntax, parameters and expected results?
Use case?

A

Return the position of specified substring in the string or last position of specified substring, respectively.

Syntax as is, and used to find the index of a char

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

What does the str.includes() str? syntax?

syntax, parameters and expected results?
Use case?

A

Returns whether or not the string starts, ends or contains a specified string.

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

What does the str.concat() method do? syntax?

syntax, parameters and expected results?
Use case?

A

Combines the text of two strings and returns a new string. string.concat(str,…str)

Used to combine two strings, result is a new string, can take a spread operator

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

What does the str.split() method do? syntax?

syntax, parameters and expected results?
Use case?

A

string.split(/regexp/,splitLimit)

Splits a String object into an array of strings by separating the string into substrings.

used to split a string into an array, you can pass a limit as a second variable to limit how many splits to make in the array leftover values are not included in the new result.

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

What does the str.slice() method do?

syntax, parameters and expected results?
Use case?

A

Extracts a section of a string and returns a new string.

str.slice(beginIndexIncluded,endIndexExcluded)

used when we want to retrieve part of a string.

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

How do we repeat a string literal?

syntax, parameters and expected results?
Use case?

A

str.repeat(n); or This is a ${name}.repeat(n);
Returns a string consisting of the elements of the object repeated the given times.

Used when we want to repeat stuff!

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

How to capitalize a string and or make it all lower case, or just the first letter?

syntax, parameters and expected results?
Use case?

A

str.toLowerCase()

str[0].toUpperCase()+str.slice(1);

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

what will str=’test’; return test[0] return?

What will return ‘test2’[0] result in?

A

They both will just return the first character of the string.

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

What is the object & methods used to turn a variable into a string?

syntax, parameters and expected results?
Use case?

A

String(string) and var.toString();

Will return a new value as a string.

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

What are the properties methods that we can use on both arrays and strings?

syntax, parameters and expected results?
Use case?

A
includes(string)
slice(firstindex(included),lastindex(excluded))
indexOf()
lastIndexOf()
.length
.concat();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the methods we can use on strings that can take regexp?

syntax, parameters and expected results for each?
Use case for each?

A

string.search(/regexp/)
returns the index of regexp match or -1

string.match(/regexp/),
returns array of matches or null

string.replace(/regexp/ORsubstr, newsubstr OR function) important refer to docs for replacement parameters

returns a string with all all or some replacements

/regexp/ORstr.test(substring)
returns true or false depending on if the string is found in the Regexp can be powerful if used in place of if statements for matches!!

string.split(/regexp/)
returns a new split array

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

What is the method to retrieve an array of results that pass a regex or string test?

syntax, parameters and expected results?
Use case?

A

string.split(/regexp/)

Splits a String object into an array of strings by separating the string into substrings.

used to split a string into an array, you can pass a limit as a second variable to limit how many splits to make in the array leftover values are not included in the new result.

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

What is the method used to change a specific value in a string?

syntax, parameters and expected results?
Use case?

A

string.replace(/regexp/,replacement)

Use to replace 1 or all matches in string, returns a new string!

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

What is the method used to remove spaces from the beginning or end of a string?

syntax, parameters and expected results?
Use case?

A

string.trim(), string.trimEnd(), string.trimStart();

Used to get rid of spaces at the end or beginning of a string.