Strings Flashcards
What are some examples of String literals?
‘String text’
“String text”
“Charactersinanylanguage”
How would you create a string using the String global object directly?
String(thing)
Parameters:
thing - anything to be converted to a string.
Since ECMAScript 2015, what can string literals be called?
Template strings
What is Escape notation and why use it?
Escape notation: \
Beside regular, printable characters, special characters can be encoded using escape notation.
Ex: \n for a new line. ' for a single quote
Does JavaScript differentiate between single quotes(‘’) and double quotes(“”)?
JavaScript makes no distinction between single or double quotes.
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.
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 \ …”;
What are strings most useful for?
Strings are most useful for holding data that can be represented in text form.
You can use the operators + and += on strings?
True.
What are the two ways to access an individual character in a string?
charAt()
Ex: return ‘cat’.charAt(1); // returns “a”
‘someString’[1]
Ex: return ‘cat’[1]; // returns “a”
What is a String?
The String global object is a constructor for strings, or a sequence of characters.
What is the most common way to compare strings?
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.
What does localeCompare() method do? and what is its syntax?
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
True / False:
JavaScript distinguishes between String objects and primitive string values.
True
What are string primitives?
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
True / False:
JavaScript does not automatically converts primitives to String objects, so that it’s possible to use String object methods for primitive strings
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 do you convert a String Object to its primitive counterpart?
A String object can always be converted to its primitive counterpart with the valueOf() method.
How would you add a property to the String Object?
String.prototype
Allows the addition of properties to a String object.
What does the method String.formCharCode() do? And What is an example?
String.fromCharCode():
Returns a string created by using the specified sequence of Unicode values.
Example:
String.fromCharCode(65); // “A”
What does the property String.prototype.length do? And what is an example?
String.prototype.length
Reflects the length of the string.
Example:
‘String’.length; // 6
What does the method String.prototype.charAt() do? And What is an example?
String.prototype.charAt():
Returns the character at the specified index.
Example:
‘String’.charAt(4); // ‘n’
What does the method String.prototype.charCodeAt() do? And What is an example?
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)
What does the method String.prototype.concat() do? And What is an example?
String.prototype.concat():
Combines the text of two strings and returns a new string.
Example:
‘String’.concat(‘ love’); // ‘String love’
What does the method String.prototype.includes() do? And What is an example?
String.prototype.includes():
Determines whether one string may be found within another string.
Example:
‘String’.includes(‘ring’); // true
‘Blue Whale’.includes(‘blue’); // false
What does the method String.prototype.endsWith() do? And What is an example?
String.prototype.endsWith():
Determines whether a string ends with the characters of another string.
Example:
‘String’.endsWith(‘ng’); // true
‘String’.endsWith(‘ri’); // false
What does the method String.prototype.indexOf() do? And What is an example?
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
What does the method String.prototype.lastIndexOf() do? And What is an example?
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
What does the method String.prototype.match() do? And What is an example?
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’]
What does the method String.prototype.repeat() do? And What is an example?
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’
What does the method String.prototype.replace() do? And What is an example?
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…
What does the method String.prototype.search() do? And What is an example?
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
What does the method String.prototype.slice() do? And What is an example?
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
What does the method String.prototype.split() do? And What is an example?
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”]
What does the method String.prototype.startsWith() do? And What is an example?
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
What does the method String.prototype.substr() do? And What is an example?
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’
What does the method String.prototype.substring() do? And What is an example?
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));
What does the method String.prototype.toLowerCase() do? And What is an example?
String.prototype.toLowerCase():
Returns the calling string value converted to lower case.
Example:
‘STRING’.toLowerCase(); // ‘string’
What does the method String.prototype.toString() do? And What is an example?
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’
What does the method String.prototype.toUpperCase() do? And What is an example?
String.prototype.toUpperCase():
Returns the calling string value converted to uppercase.
Example:
‘alphabet’.toUpperCase(); // ‘ALPHABET’
What does the method String.prototype.trim() do? And What is an example?
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’
What does the method String.prototype.valueOf() do? And What is an example?
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’