String Methods Flashcards

1
Q

Return string with spaces removed from beginning and end from an old string

Return a string with spaces removed from the beginning (2 ways)

Return a string with spaces removed from the end

A

myString.trim()

myString.trimStart()
or trimLeft()
myString.trimEnd()
or trimRight()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Return string all upper case or lower case from an old string

A

myString.toUpperCase()

myString.toLowerCase()

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

(3 methods)

Parse string for something, return boolean if it’s there, 2 ways

  • How do they each respond to regex?
  • Can they search parts of arrays?

There’s a third way that returns an array of what it finds and/or info about what it finds.

A

/thingsToTestFor/.test(string or array to search in)
- HAS to use regex
- Can search parts of elements in arrays.
- searches within all levels of nested arrays
Regex String

myStringOrArray.includes()
- CAN’T use regex
- Can’t search for parts of elements in arrays
String String

myString.match( )
- It can use regex
- returns either the element or an array with all instances of the element with the global tag
String Regex

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

Parse strings for something and return an array of:

The matched string, the Index# where the substring starts, the original string, groups

A

myString.match()

With g flag it returns an array with all instances of the find.

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

Split a string into an array. Splitsat the argumentstring characters

A

myString.split(‘ ‘, number of items to split)

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

Find a substring within a string. Replace it with something. Return the new string.

How does it respond to regex?

A

myString.replace(thingToFind, thingToReplace)

Can use / / g with thingToFind to replace all instances

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

Return part of a string based on index positions of characters (2 ways)

A

myString.substring(startIndex, endIndexExclusive)

myString.slice()

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

Concatenate a string 2 ways

A

‘string 1’ + ‘ string 2’

‘string 1’.concat(‘ string 2’, ‘string3’)

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

What is logged from:

let apply = ‘012345’;

console.log(apply.substring(4,1));

A

‘123’
substring should be (start index inc, end index exc)
It swaps the arguments in .substring() when the start index > end index

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

What does
String.prototype.trim()
do?
and/or return?

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.).

trim()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What does 
String.prototype.trimStart() 
String.prototype.trimLeft()
do?
and/or return?
A

The trimStart() method removes whitespace from the beginning of a string. trimLeft() is an alias of this method.

trimStart()

trimLeft()

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

What does
String.prototype.trimEnd()
String.prototype.trimRight()
do?

A

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

trimEnd()
trimRight()

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

What does
String.prototype.toUpperCase()
do?

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).

toUpperCase()

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

What does
String.prototype.toLowerCase()
do?
and/or return?

A

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

toLowerCase()

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

What does
RegExpObject.test()
do?
and/or return?

A

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false. Regex String

test(str)

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

What does
String.prototype.includes()
do?
and/or return?

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. String String

includes(searchString)
includes(searchString, position)

(There is also an Array.prototype.includes())

17
Q

What does
String.prototype.match()
do?
and/or return?

A

The match() method retrieves the result of matching a string against a regular expression. String Regex

Returns an array of info:
[ ‘a’, index: 0, input: ‘apple’, groups: undefined ]

with g flag it returns an array of all found instances

match(regexp)

18
Q

What does
String.prototype.split()
do?
and/or return?

A

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.

split()
split(separator)
split(separator, limit#ofSplits)

19
Q

What does
String.prototype.replace()
do?
and/or return?

A

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

The original string is left unchanged.

replace(regexp, newSubstr)
replace(regexp, replacerFunction)

replace(substr, newSubstr)
replace(substr, replacerFunction)

20
Q

What does
String.prototype.substring()
do?
and/or return?

A

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

substring(indexStart)
substring(indexStart, indexEnd)

21
Q

What does
String.prototype.concat()
do?
and/or return?

A

The concat() method concatenates the string arguments to the calling string and returns a new string.

concat(str1)
concat(str1, str2)
concat(str1, str2, … , strN)

(There is also an Array.prototype.concat())

22
Q

What does
String.prototype.charAt()
do?
and/or return?

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.

charAt(index)

23
Q

What’s the difference between:
String.prototype.includes()
String.prototype.match()
RegExpObject.prototype.test()

A

.includes() - 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.
String String

.match() - returns an array of strings against a regular expression
String Regex

RegExpObject.test() - returns boolean if a string argument is in the the regular expression
Regex String

24
Q

What does
String.prototype.indexOf()
do?
and/or return?

A

The indexOf() 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.

indexOf(searchValue)
indexOf(searchValue, fromIndex)

let str = ‘Widget with id’;

alert( str.indexOf(‘Widget’) ); // 0, because ‘Widget’ is found at the beginning
alert( str.indexOf(‘widget’) ); // -1, not found, the search is case-sensitive

25
Q

What does
String.prototype.startsWith()
do?

A

Returns true if a string begins with the argument characters

26
Q

What does
String.prototype.endsWith()
do?

A

Returns true if a string ends with the argument characters

27
Q

Return true if a string begins with the argument characters

A

String.prototype.startsWith()

28
Q

Return true if a string ends with the argument characters

A

String.prototype.endsWith()

29
Q

What does
String.prototype.substr()
do?

A

Returns a section of a string (using first index and number of characters rather than last index)

The first argument may be negative, to count from the end:
alert( str.substr(-4, 2) ); // ‘gi’, from the 4th position get 2 characters

30
Q

Return a section of a string (using first index and number of characters rather than last index. also negative indexes will count from the end. The last index being -1)

A

String.prototype.substr()

31
Q

Drawback of String.prototype.substr()

A

Formally, substr has a minor drawback: it is described not in the core JavaScript specification, but in Annex B, which covers browser-only features that exist mainly for historical reasons. So, non-browser environments may fail to support it. But in practice it works everywhere.

32
Q

Compare 2 strings for alphabetical order rather than unicode numerical order

Returns a negative number if str1 is less than str2.
Returns a positive number if str1 is greater than str2.
Returns 0 if they are equivalent.

A

String.prototype.localeCompare()

const a = 'réservé'; // with accents, lowercase
const b = 'RESERVE'; // no accents, uppercase

[[if > is true return 1. If < is true return -1. If equal return 0.]]

console.log(a.localeCompare(b));
// expected output: 1
console.log(a.localeCompare(b, 'en', { sensitivity: 'base' }));
// expected output: 0
33
Q

What does
String.prototype.localeCompare()
do and/or return?

A

Compare 2 strings for alphabetical order rather than unicode numerical order

Returns a negative number if str is less than str2.
Returns a positive number if str is greater than str2.
Returns 0 if they are equivalent.

[[if > is true return 1. If < is true return -1. If equal return 0.]]

const a = 'réservé'; // with accents, lowercase
const b = 'RESERVE'; // no accents, uppercase
console.log(a.localeCompare(b));
// expected output: 1
console.log(a.localeCompare(b, 'en', { sensitivity: 'base' }));
// expected output: 0
34
Q

What’s the similaritiesdifference between
String.prototype.slice()
String.prototype.substr()
String.prototype.substring()

in terms of:

  • Start End
  • Purpose
  • Mutation
  • Second argument smaller
  • first argument requirement
  • second argument requirement
A

Start End

string. slice(start,end)
string. substr(start,length)
string. substring(start,end)

Purpose
The slice() method extracts parts of a string and returns the extracted parts in a new string.
The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
The substring() method extracts parts of a string and returns the extracted parts in a new string.

Mutation
slice() Doesn’t
substr() Doesn’t
substring() Doesn’t

Negative Numbers
slice() selects characters starting from the end of the string
substr()selects characters starting from the end of the string
substring() Doesn’t Perform (just treats negatives as 0)

Second argument smaller
slice() Doesn’t Perform (returns an empty string)
substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems
substring() will swap the two arguments, and perform as usual

First argument requirement
slice() Required, indicates: Starting Index
substr() Required, indicates: Starting Index
substring() Required, indicates: Starting Index

Second argument requirement
slice() Optional, The position (up to, but not including) where to end the extraction
substr() Optional, The number of characters to extract
substring() Optional, The position (up to, but not including) where to end the extraction
IF OMITTED
slice() selects all characters from the start-position to the end of the string
substr() selects all characters from the start-position to the end of the string
substring() selects all characters from the start-position to the end of the string