Strings Flashcards

1
Q

What are some examples of String literals?

A

‘String text’
“String text”
“Charactersinanylanguage”

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

How would you create a string using the String global object directly?

A

String(thing)

Parameters:

thing - anything to be converted to a string.

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

Since ECMAScript 2015, what can string literals be called?

A

Template strings

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

What is Escape notation and why use it?

A

Escape notation: \

Beside regular, printable characters, special characters can be encoded using escape notation.

Ex: \n for a new line. ' for a single quote

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

Does JavaScript differentiate between single quotes(‘’) and double quotes(“”)?

A

JavaScript makes no distinction between single or double quotes.

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

What two ways can you combine long literal strings, such as this question, to keep from having lines that go on endlessly?

You would use these techniques to make your code more readable.

A

Either use the + operator or a \ at the end of each line. The \ cannot have any spaces immediately after it.

Ex1: “What two ways can you combine long “ + “literal strings, such as this question, to “…”;

Ex2: “What two ways can you combine long \
literal strings, such as this question, to \ …”;

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

What are strings most useful for?

A

Strings are most useful for holding data that can be represented in text form.

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

You can use the operators + and += on strings?

A

True.

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

What are the two ways to access an individual character in a string?

A

charAt()

Ex: return ‘cat’.charAt(1); // returns “a”

‘someString’[1]

Ex: return ‘cat’[1]; // returns “a”

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

What is a String?

A

The String global object is a constructor for strings, or a sequence of characters.

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

What is the most common way to compare strings?

A

Using the less than operators.

Less common option but still valid:
A similar result can be achieved using the localeCompare() method inherited by String instances.

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

What does localeCompare() method do? and what is its syntax?

A

The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

Syntax:
referenceStr.localeCompare(compareString[, locales[, options]])

Example:
// The letter “a” is before “c” yielding a negative value
‘a’.localeCompare(‘c’); // -2 or -1

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

True / False:

JavaScript distinguishes between String objects and primitive string values.

A

True

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

What are string primitives?

A

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive

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

True / False:

JavaScript does not automatically converts primitives to String objects, so that it’s possible to use String object methods for primitive strings

A

False

In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

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

How do you convert a String Object to its primitive counterpart?

A

A String object can always be converted to its primitive counterpart with the valueOf() method.

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

How would you add a property to the String Object?

A

String.prototype

Allows the addition of properties to a String object.

18
Q

What does the method String.formCharCode() do? And What is an example?

A

String.fromCharCode():

Returns a string created by using the specified sequence of Unicode values.

Example:
String.fromCharCode(65); // “A”

19
Q

What does the property String.prototype.length do? And what is an example?

A

String.prototype.length
Reflects the length of the string.

Example:
‘String’.length; // 6

20
Q

What does the method String.prototype.charAt() do? And What is an example?

A

String.prototype.charAt():
Returns the character at the specified index.

Example:
‘String’.charAt(4); // ‘n’

21
Q

What does the method String.prototype.charCodeAt() do? And What is an example?

A

String.prototype.charCodeAt():
Returns a number indicating the Unicode value of the character at the given index.

Example:
‘String’.charCodeAt(2); // 114 (unicode value of r)

22
Q

What does the method String.prototype.concat() do? And What is an example?

A

String.prototype.concat():
Combines the text of two strings and returns a new string.

Example:
‘String’.concat(‘ love’); // ‘String love’

23
Q

What does the method String.prototype.includes() do? And What is an example?

A

String.prototype.includes():
Determines whether one string may be found within another string.

Example:
‘String’.includes(‘ring’); // true
‘Blue Whale’.includes(‘blue’); // false

24
Q

What does the method String.prototype.endsWith() do? And What is an example?

A

String.prototype.endsWith():
Determines whether a string ends with the characters of another string.

Example:
‘String’.endsWith(‘ng’); // true
‘String’.endsWith(‘ri’); // false

25
Q

What does the method String.prototype.indexOf() do? And What is an example?

A

String.prototype.indexOf():
Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

Example:
‘Blue Whale’.indexOf(‘Blue’); // returns 0

26
Q

What does the method String.prototype.lastIndexOf() do? And What is an example?

A

String.prototype.lastIndexOf():
Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

Example:
‘canal’.lastIndexOf(‘a’); // returns 3

27
Q

What does the method String.prototype.match() do? And What is an example?

A

String.prototype.match():
Used to match a regular expression against a string.

Example:
var str = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu’;
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);
console.log(matches_array); // [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

28
Q

What does the method String.prototype.repeat() do? And What is an example?

A
String.prototype.repeat():
The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

Example:
‘abc’.repeat(1); // ‘abc’
‘abc’.repeat(2); // ‘abcabc’

29
Q

What does the method String.prototype.replace() do? And What is an example?

A
String.prototype.replace():
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.

Example:
var str = ‘Twas the night before Xmas…’;
var newstr = str.replace(/xmas/i, ‘Christmas’);
console.log(newstr); // Twas the night before Christmas…

30
Q

What does the method String.prototype.search() do? And What is an example?

A

String.prototype.search():
Executes the search for a match between a regular expression and a specified string.

Example:
‘string’.search(‘m’); // -1
‘string.search(‘n’); // 0

31
Q

What does the method String.prototype.slice() do? And What is an example?

A

String.prototype.slice():
Extracts a section of a string and returns a new string.

Example:
var str1 = ‘The morning is upon us.’;
var str2 = str1.slice(4, -2);
console.log(str2); // OUTPUT: morning is upon u

32
Q

What does the method String.prototype.split() do? And What is an example?

A

String.prototype.split():
Splits a String object into an array of strings by separating the string into substrings.

Example:
‘string’.split(‘r’); // [“st”, “ing”]
“abcabcabcabcabc”.split(‘c’, 2); // [“ab”, “ab”]

33
Q

What does the method String.prototype.startsWith() do? And What is an example?

A

String.prototype.startsWith():
Determines whether a string begins with the characters of another string.

Example:
//startswith
var str = 'To be, or not to be, that is the question.';

console. log(str.startsWith(‘To be’)); // true
console. log(str.startsWith(‘not to be’)); // false
console. log(str.startsWith(‘not to be’, 10)); // true

34
Q

What does the method String.prototype.substr() do? And What is an example?

A

String.prototype.substr():
Returns the characters in a string beginning at the specified location through the specified number of characters.

Example:
var str = 'abcdefghij';

console. log(‘(1, 2): ‘ + str.substr(1, 2)); // ‘(1, 2): bc’
console. log(‘(-3, 2): ‘ + str.substr(-3, 2)); // ‘(-3, 2): hi’

35
Q

What does the method String.prototype.substring() do? And What is an example?

A

String.prototype.substring():
Returns the characters in a string between two indexes into the string.

Example:
var anyString = 'Mozilla';

// Displays ‘Moz’

console. log(anyString.substring(0, 3));
console. log(anyString.substring(3, 0));

36
Q

What does the method String.prototype.toLowerCase() do? And What is an example?

A

String.prototype.toLowerCase():
Returns the calling string value converted to lower case.

Example:
‘STRING’.toLowerCase(); // ‘string’

37
Q

What does the method String.prototype.toString() do? And What is an example?

A

String.prototype.toString():
The String object overrides the toString() method of the Object object; it does not inherit Object.prototype.toString(). For String objects, the toString() method returns a string representation of the object and is the same as the String.prototype.valueOf() method.

Example:
var x = new String('Hello world');

console.log(x.toString()); // logs ‘Hello world’

38
Q

What does the method String.prototype.toUpperCase() do? And What is an example?

A

String.prototype.toUpperCase():
Returns the calling string value converted to uppercase.

Example:
‘alphabet’.toUpperCase(); // ‘ALPHABET’

39
Q

What does the method String.prototype.trim() do? And What is an example?

A

String.prototype.trim():
Trims whitespace from the beginning and end of the string. Part of the ECMAScript 5 standard.

Example:
var orig = ‘ foo ‘;
console.log(orig.trim()); // ‘foo’

40
Q

What does the method String.prototype.valueOf() do? And What is an example?

A
String.prototype.valueOf():
The valueOf() method of String returns the primitive value of a String object as a string data type. This value is equivalent to String.prototype.toString().

Example:
var x = new String(‘Hello world’);
console.log(x.valueOf()); // Displays ‘Hello world’