JS String Methods Flashcards

1
Q

String.prototype.toUpperCase()

A

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

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

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

String.prototype.toLowerCase()

A

The toLowerCase() method returns the calling string value converted to lower case.

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

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

String.prototype.indexOf()

A

The indexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring. Given a second argument: a number, the method returns the first occurrence of the specified substring at an index greater than or equal to the specified number.

Note: Returns -1 if argument is not found.

Syntax
indexOf(searchString)
indexOf(searchString, position)

Examples
Using indexOf()
The following example uses indexOf() to locate substrings in the string “Brave new world”.

const 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
4
Q

String.prototype.lastIndexOf()

A

The lastIndexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the last occurrence of the specified substring. Given a second argument: a number, the method returns the last occurrence of the specified substring at an index less than or equal to the specified number.

Note: Returns -1 if argument is not found.

Syntax
lastIndexOf(searchString)
lastIndexOf(searchString, position)

Examples
Using indexOf() and lastIndexOf()
The following example uses indexOf() and lastIndexOf() to locate values in the string “Brave, Brave New World”.

const anyString = ‘Brave, Brave New World’;

console.log(The index of the first "Brave" is ${anyString.indexOf('Brave')});
// logs 0
console.log(The index of the last "Brave" is ${anyString.lastIndexOf('Brave')});
// logs 7

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

String.prototype.charAt()

A

The String object’s charAt() method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string.

Syntax
charAt(index)

Examples
Displaying characters at different locations in a string
The following example displays characters at different locations in the string “Brave new world”:

const anyString = ‘Brave new world’;
console.log(The character at index 0 is '${anyString.charAt()}');
// No index was provided, used 0 as default

console.log(The character at index 0 is '${anyString.charAt(0)}');
console.log(The character at index 1 is '${anyString.charAt(1)}');
console.log(The character at index 2 is '${anyString.charAt(2)}');
console.log(The character at index 3 is '${anyString.charAt(3)}');
console.log(The character at index 4 is '${anyString.charAt(4)}');
console.log(The character at index 999 is '${anyString.charAt(999)}');

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

String.prototype.charCodeAt()

A

The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

Syntax
charCodeAt(index)

Examples
Using charCodeAt()
The following example returns 65, the Unicode value for A.

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

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

String.prototype.replace()

A

The replace() method returns a new string with one, 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 called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.

Syntax
replace(pattern, replacement)

Examples
Defining the regular expression in replace()
In the following example, the regular expression is defined in replace() and includes the ignore case flag.

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

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

String.prototype.split()

A

The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

Syntax
split()
split(separator)
split(separator, limit)

Examples
Using split()
When the string is empty and no separator is specified, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.

const emptyString = ‘’;

// string is empty and no separator is specified
console.log(emptyString.split());
// [””]

// string and separator are both empty strings
console.log(emptyString.split(emptyString));
// []

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

String.prototype.substring()

A

The substring() method returns the part of the string between the start and end indexes, or to the end of the string.

Syntax
substring(indexStart)
substring(indexStart, indexEnd)

Examples
Using substring()
The following example uses substring() to display characters from the string ‘Mozilla’:

const anyString = ‘Mozilla’;

// Displays ‘M’
console.log(anyString.substring(0, 1));
console.log(anyString.substring(1, 0));

// Displays ‘Mozill’
console.log(anyString.substring(0, 6));

// Displays ‘lla’
console.log(anyString.substring(4));
console.log(anyString.substring(4, 7));
console.log(anyString.substring(7, 4));

// Displays ‘Mozilla’
console.log(anyString.substring(0, 7));
console.log(anyString.substring(0, 10));

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

String.prototype.search()

A

The search() method executes a search for a match between a regular expression and this String object.

Syntax
search(regexp)

Examples
Using search()
The following example searches a string with two different regex objects to show a successful search (positive value) vs. an unsuccessful search (-1).

const str = “hey JudE”;
const re = /[A-Z]/;
const reDot = /[.]/;
console.log(str.search(re)); // returns 4, which is the index of the first capital letter “J”
console.log(str.search(reDot)); // returns -1 cannot find ‘.’ dot punctuation

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

String.prototype.trim()

A

The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original 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.).

Syntax
trim()

Examples
Using trim()
The following example displays the lowercase string ‘foo’:

const orig = ‘ foo ‘;
console.log(orig.trim()); // ‘foo’

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

String.prototype.slice()

A

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

Syntax
slice(indexStart)
slice(indexStart, indexEnd)

Examples
Using slice() to create a new string
The following example uses slice() to create a new string.

const 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: “”

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

String.prototype.includes()

A

The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.

Syntax
includes(searchString)
includes(searchString, position)

Examples
Using includes()
const 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
console.log(str.includes(‘’)) // true

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

String.prototype.startsWith()

A

The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

Syntax
startsWith(searchString)
startsWith(searchString, position)

Examples
Using startsWith()
const 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

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

String.prototype.endsWith()

A

The endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

Syntax
endsWith(searchString)
endsWith(searchString, endPosition)

Examples
Using endsWith()
const str = ‘To be, or not to be, that is the question.’;

console.log(str.endsWith(‘question.’)); // true
console.log(str.endsWith(‘to be’)); // false
console.log(str.endsWith(‘to be’, 19)); // true

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