String Flashcards

1
Q

Method returns a string created from the specified sequence of UTF-16 code units.

A
console.log(String.fromCharCode(189, 43, 190, 61));
// expected output: "½+¾="
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Method returns a string created by using the specified sequence of code points.

A
console.log(String.fromCodePoint(9731, 9733, 9842, 0x2F804));
// expected output: "☃★♲你"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string.

A

var sentence = ‘The quick brown fox jumped over the lazy dog.’;

var index = 4;

console.log('The character at index ' + index + ' is ' + sentence.charAt(index));
// expected output: "The character at index 4 is q"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

A

‘ABC’.charCodeAt(0); // returns 65

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

Method returns a non-negative integer that is the Unicode code point value.

A

‘ABC’.codePointAt(1); // 66
‘\uD800\uDC00’.codePointAt(0); // 65536

‘XYZ’.codePointAt(42); // undefined

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

Method concatenates the string arguments to the calling string and returns a new string.

A
var str1 = 'Hello';
var str2 = 'World';
console.log(str1.concat(' ', str2));
// expected output: "Hello World"
console.log(str2.concat(', ', str1));
// expected output: "World, Hello"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

A

const str1 = ‘Cats are the best!’;

console.log(str1.endsWith('best', 17));
// expected output: true

const str2 = ‘Is this a question’;

console.log(str2.endsWith('?'));
// expected output: false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Method determines whether one string may be found within another string, returning true or false as appropriate.

A

var str = ‘To be, or not to be, that is the question.’;

console. log(str.includes(‘To be’)); // true
console. log(str.includes(‘question’)); // true
console. log(str.includes(‘nonexistent’)); // false
console. log(str.includes(‘To be’, 1)); // false
console. log(str.includes(‘TO BE’)); // false

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

Method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

A

var str = ‘Brave new world’;

console. log(‘Index of first w from start is ‘ + str.indexOf(‘w’)); // logs 8
console. log(‘Index of “new” from start is ‘ + str.indexOf(‘new’)); // logs 6

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

Method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found.

A

var anyString = ‘Brave new world’;

console.log(‘The index of the first w from the beginning is ‘ + anyString.indexOf(‘w’));
// logs 8
console.log(‘The index of the first w from the end is ‘ + anyString.lastIndexOf(‘w’));
// logs 10
console.log(‘The index of “new” from the beginning is ‘ + anyString.indexOf(‘new’));
// logs 6
console.log(‘The index of “new” from the end is ‘ + anyString.lastIndexOf(‘new’));
// logs 6

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

Method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

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.

var a = 'réservé'; // with accents, lowercase
var b = 'RESERVE'; // no accents, uppercase
console.log(a.localeCompare(b));
// expected output: 1
console.log(a.localeCompare(b, 'en', {sensitivity: 'base'}));
// expected output: 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Method retrieves the matches when matching a string against a regular expression.

A
var paragraph = 'The quick brown fox jumped over the lazy dog. It barked.';
var regex = /[A-Z]/g;
var found = paragraph.match(regex);
console.log(found);
// expected output: Array ["T", "I"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.

A

‘abc’.padEnd(10); // “abc “
‘abc’.padEnd(10, “foo”); // “abcfoofoof”
‘abc’.padEnd(6, “123456”); // “abc123”
‘abc’.padEnd(1); // “abc”

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

Method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start (left) of the current string.

A

‘abc’.padStart(10); // “ abc”
‘abc’.padStart(10, “foo”); // “foofoofabc”
‘abc’.padStart(6,”123465”); // “123abc”
‘abc’.padStart(8, “0”); // “00000abc”
‘abc’.padStart(1); // “abc”

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

Method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

A
'abc'.repeat(-1);   // RangeError
'abc'.repeat(0);    // ''
'abc'.repeat(1);    // 'abc'
'abc'.repeat(2);    // 'abcabc'
'abc'.repeat(3.5);  // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0);  // RangeError
({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2);
// 'abcabc' (repeat() is a generic method)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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. If pattern is a string, only the first occurrence will be replaced.

A
var str = 'Twas the night before Xmas...';
var newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);  // Twas the night before Christmas...
17
Q

Method executes a search for a match between a regular expression and this String object.

A
var str = "hey JudE";
var re = /[A-Z]/g;
var re2 = /[.]/g;
console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J"
console.log(str.search(re2)); // returns -1 cannot find '.' dot punctuation
18
Q

Method extracts a section of a string and returns it as a new string, without modifying the original string.

A

var str1 = ‘The morning is upon us.’, // the length of str1 is 23.
str2 = str1.slice(1, 8),
str3 = str1.slice(4, -2),
str4 = str1.slice(12),
str5 = str1.slice(30);
console.log(str2); // OUTPUT: he morn
console.log(str3); // OUTPUT: morning is upon u
console.log(str4); // OUTPUT: is upon us.
console.log(str5); // OUTPUT: “”

19
Q

Method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

A
function splitString(stringToSplit, separator) {
  var arrayOfStrings = stringToSplit.split(separator);

console.log(‘The original string is: “’ + stringToSplit + ‘”’);
console.log(‘The separator is: “’ + separator + ‘”’);
console.log(‘The array has ‘ + arrayOfStrings.length + ‘ elements: ‘ + arrayOfStrings.join(‘ / ‘));
}

var tempestString = 'Oh brave new world that has such people in it.';
var monthString = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec';
var space = ' ';
var comma = ',';

splitString(tempestString, space);
splitString(tempestString);
splitString(monthString, comma);

20
Q

Method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

A
//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

21
Q

Method returns the part of the string between the start and end indexes, or to the end of the string.

A

var str = ‘Mozilla’;

console.log(str.substring(1, 3));
// expected output: "oz"
console.log(str.substring(2));
// expected output: "zilla"
22
Q

Method returns the calling string value converted to lower case, according to any locale-specific case mappings.

A

‘ALPHABET’.toLocaleLowerCase(); // ‘alphabet’

‘\u0130’.toLocaleLowerCase(‘tr’) === ‘i’; // true
‘\u0130’.toLocaleLowerCase(‘en-US’) === ‘i’; // false

let locales = ['tr', 'TR', 'tr-TR', 'tr-u-co-search', 'tr-x-turkish'];
'\u0130'.toLocaleLowerCase(locales) === 'i'; // true
23
Q

Method returns the calling string value converted to upper case, according to any locale-specific case mappings.

A

‘alphabet’.toLocaleUpperCase(); // ‘ALPHABET’

‘Gesäß’.toLocaleUpperCase(); // ‘GESÄSS’

‘i\u0307’.toLocaleUpperCase(‘lt-LT’); // ‘I’

let locales = [‘lt’, ‘LT’, ‘lt-LT’, ‘lt-u-co-phonebk’, ‘lt-x-lietuva’];
‘i\u0307’.toLocaleUpperCase(locales); // ‘I’

24
Q

Method returns the calling string value converted to lower case.

A

console.log(‘ALPHABET’.toLowerCase()); // ‘alphabet’

25
Q

Method returns a string representing the specified object.

A

var x = new String(‘Hello world’);

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

26
Q

Method returns the calling string value converted to uppercase (the value will be converted to a string if it isn’t one).

A

console.log(‘alphabet’.toUpperCase()); // ‘ALPHABET’

27
Q

Method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

A

var greeting = “ Hello world! “;

console.log(greeting);
// expected output: "   Hello world!   ";
console.log(greeting.trim());
// expected output: "Hello world!";
28
Q

Method removes whitespace from the end of a string. trimRight() is an alias of this method.

A

var greeting = “ Hello world! “;

console.log(greeting);
// expected output: "   Hello world!   ";
console.log(greeting.trimEnd());
// expected output: "   Hello world!";
29
Q

Method removes whitespace from the beginning of a string.

A

var greeting = “ Hello world! “;

console.log(greeting);
// expected output: "   Hello world!   ";
console.log(greeting.trimStart());
// expected output: "Hello world!   ";
30
Q

Method returns the primitive value of a String object.

A
var x = new String('Hello world');
console.log(x.valueOf()); // Displays 'Hello world'
31
Q

Method returns a new Iterator object that iterates over the code points of a String value, returning each code point as a String value.

A

var str = ‘A\uD835\uDC68’;

var strIter = strSymbol.iterator;

console. log(strIter.next().value); // “A”
console. log(strIter.next().value); // “\uD835\uDC68”

32
Q

Method is a tag function of template literals, similar to the r prefix in Python or the @ prefix in C# for string literals (yet there is a difference: see explanations in this issue). It’s used to get the raw string form of template strings, that is, substitutions (e.g. ${foo}) are processed, but escapes (e.g. \n) are not.

A
String.raw`Hi\n${2+3}!`;
// 'Hi\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
33
Q

Method returns the Unicode Normalization Form of a given string (if the value isn’t a string, it will be converted to one first).

A
var first = '\u212B';         // "Å"
var second = '\u0041\u030A';  // "Å"

console.log(first + ‘ and ‘ + second + ‘ are’ +
((first === second)? ‘’: ‘ not’) + ‘ the same.’);
// expected output: “Å and Å are not the same.”

console.log(first + ‘ and ‘ + second + ‘ can’ +
((first.normalize(‘NFC’) === second.normalize(‘NFC’))? ‘’: ‘ not’) + ‘ be normalized’);
// expected output: “Å and Å can be normalized”

var oldWord = 'mañana';
var newWord = oldWord.normalize('NFD');
console.log('The word did ' + ((oldWord != newWord)? 'not ' : '') + 'change.');
// expected output: "The word did change."