String Methods Flashcards

1
Q

Length

A

Find the length of a string
var.length

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

.trim()

A

Removing white space
Used when processing the string of a user input field

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

.includes()

A

Determines if string contains substring
returns true or false
Used for string matching or searching/parsing

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

.indexOf()

A

Finding the index of a substring
returns the index of the substring within the string
if no substring, returns -1
Used to check if index is greater than -1 (aka does the substring exist within the string?) using a conditional

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

.toUpperCase()

A

returns a string with all uppercase letters

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

.toLowerCase()

A

returns string in lower case letters

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

.replace()

A

Replaces strings with new values
returns a string with a pattern replaced by a replacement string
using regex- can globally replace
using string - can only replace the first occurence

ex: const str = ‘hello world! my name is also world.”
const stringPattern = ‘world’
const regexPattern = /world/gi
const replacement = ‘gitconnected’

console.log(str.replace(stringPattern, replacement)) //Hello gitconnected! my name is also world
console.log(str.replace(regexPattern, replacement)) //Hello gitconnected! my name is also gitconnected.

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

.slice()

A
  • returns a section of a string
  • extracts a section of a string based on the index (first param is the first index, second is optional ending)
  • negative begin index: slice backwards from the end
  • can use with indexOf() as a reference point
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

.split()

A
  • converts into an array
  • This is useful when you know your string uses a certain character to separate data, or if you want to operate on specific substrings individually.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

.repeat()

A

Repeats a string a specified number of times
pass number of times as the param

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

.match()

A

Returns array of matching strings (returns the ones that match) when comparing a string against a regular expression

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

.charAt()

A

Returns a character at an index
param is the index num

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

.charCodeAt()

A

Returns the unicode of the char at a specified index
(UTF-16 cone integer between 0 and 65535.)

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