String Flashcards
Attributes
Primitive data type / immutable Contains properties & methods #safe (ES1)
Instantiation
// literal "literal" // constructor; arg is converted into string String("literal") new String("object")
Properties
.length => {num} length of string; not exact if contains uncommon chars #safe
Methods (16)
String.fromCharCode — convert char codes into string representation
.charCodeAt — numeric Unicode value @ index
.indexOf — index of first occurring substring
.lastIndexOf — index of last occurring substring
.match — array of matching substring/pattern
.replace —string w/ replacements
.search —first occurring index of pattern
.slice —substring based on start & end index; indexes can be relative to beginning or ending of string
.split —break up string into an array of strings
.substr —substring based on start index & length
.substring —substring based on start & end index
.toLowerCase —string lowercased
.toUpperCase —string uppercased
.trim —string w/ front & end whitespace stripped
.toString —same as .valueOf
.valueOf —return string in primitive form
Comparing strings with > type operators. How is comparing done? What about uppercase?
Comparisons are done lexically, so “d” is greater than “c”. Uppercase is only less than lowercase if the strings are otherwise equal.
Concatenating and comparing strings against non strings.
Concatenating a non string will first convert the value into a string. Comparing non strings against strings using the non strict equality operator (==) will also convert the non string value to a string first.
”” + undefined // => “undefined”
“” + null // => “null”
“” + true // => “true”
“” + 3.1 // => “3.1”
“” + function () { return 1; } // => “function () { return 1; }”
“” + {} // => “[object Object]”
“” + [1, 2] // => “1,2” - Arrays apply .join() w/ default arg of “,”
Extract a single character from a string by index (2)
“string”[1] // => “t”
Treat the string as an array-like object, where individual characters correspond to a numerical index.