String-Methods Flashcards
1
Q
fromCharChode
A
String.fromCharChode(num code1, ...) => {str} string created by sequence of char-codes #safe #static
2
Q
charCodeAt
A
.charCodeAt(num index) => {num} numeric Unicode value at `index` of string #safe
index
- range: 0 - length-1, else NaN returned
3
Q
indexOf
A
.indexOf(str search, num start=0) => {num} index of `search` match, else -1 #safe
start
- if < 0, search = 0
- if >= length, returns -1
4
Q
lastIndexOf
A
.lastIndexOf(str search, num start=length) => {num} index of `search` match, searched from right-to-left, else -1 #safe
start
- if < 0, search = 0
- if > length, returns -1
5
Q
match
A
.match(reg search) => {array|null} array of `search` match results, or null if none were found #safe
search
- if given a non-regexp, will convert into one via
new RegExp(search)
. - if
search
has theg
flag, the array will be matches only: [match1, match2, …] - if the
g
flag is absent, the returned array will be:[match, captureParens, ..., index: match-index, input: provided-string]
- (this is identical to regexp.exec(string))
6
Q
replace
A
.replace(str|reg search, str|func replace) => {str} string with `search` replaced by `replace` #safe
search
- if string or regexp w/o g flag, replaces at most 1 time;
- if regexp w/ g flag replaces all times.
replace - if string, can use characters \$\$ => $ $& => matched substring $` => text prior to match $' => text after match $n => capturing-parens of `search` regexp pattern - if function, will be called for every match and the returned value used as the search-replacement; arguments are identical to the pattern.exec() or str.match() array (when pattern omits g-flag): - match - matched substring - p1... - capturing parens 1, etc. (if applies) - index - index when match starts - input - input-string
7
Q
search
A
.search(reg search) => {num} index of `search` match, else -1 #safe
8
Q
slice
A
.slice(num start, num end=length) => {str} sub-string of string from start to end index #safe
start
- if < 0, start=length+start
- if > length, returns ‘’
end
- exclusive
- if < 0, end=length+end
- if > length, returns ‘’
9
Q
split
A
.split(str|reg separator, num limit=none) => {arr} array of substrings split by `separator` #safe
input
- if initial string is empty, returns [’’]
separator
- if not provided, returns array with input string as only element
- if empty string, returns array of every character
limit
- ONLY truncates the returned array afterwards
10
Q
substr
A
.substr(num start, num length=length) => {str} substring between start & length #safe
start
- if start < 0, start = length+start
- if start >= length, returns ‘’
length
- if length is
11
Q
substring
A
.substring(num start, num end=length) => {str} substring between start & end index #safe #slow - better to use slice()
start|end
- range 0 - length; if outside, converts into closest range member ( end, start & end become swapped
end
- exclusive
12
Q
toLowerCase
A
.toLowerCase() => {str} string lowercased #safe
13
Q
toUpperCase
A
.toUpperCase() => {str} string uppercased #safe
14
Q
trim
A
.trim() => {str} string with whitespace removed from beginning and end #ES5