String object

Objectives

  • Compare strings
  • Access properties and parts of a string
  • Manipulate string objects

Comparing strings

Strings can be compared using the normal relational operators: <, <=, >, >=, !=, ==

String property

  • length: returns the length of a string
  • Note, s1 will be a string primitive: var s1 = "abc";
  • Note, s2 will be a string object: var s2 = new String("abc");
  • Object vs. primitive matters if you use the eval function

String methods

  • Array subscript notation can be used to get a character at a specific position in a string: "abcd"[2] evaluates to "c"
  • charAt(n): returns the character at position n
  • concat(s): returns the concatenation of the current string and s
  • includes(s): returns true is s is found within the current string
  • endsWith(s): returns true if the current string ends with s
  • indexOf(s): returns the position of the first occurrence of s
  • lastIndexOf(s): returns the position of the last occurrence of s
  • match(s1, s2): returns a string with matches of string s1 replaced by string s2
  • match(regex, s2): returns a string with matches of regex replaced by string s2
  • padEnd(n [,s]): returns a string padded on the right to get it to length n
  • padStart(n [,s]): returns a string padded on the left to get it to length n
  • repeat(n): returns a string which is n copies of the current string concatenated
  • search(regex): returns position of first match of regex
  • slice(begin [, end]): returns portion of string starting at begin and going until just before end
  • split(delim): returns an array of tokens based on using delim as a delimiter on the current string, where delim may be a string or a regular expression
  • startsWith(s): returns true if the current string starts with s
  • substr(start [,len]): returns the substring starting at position start which is len characters long
  • substring(ndx1 [,ndx2]): returns the substring starting between positions ndx1 and ndx2 (not including ndx2)
  • toLowerCase(): returns the lowercase version of the current string
  • toUpperCase(): returns the uppercase version of the current string
  • trim(): returns a copy of the current string with whitespace trimmed from the front and back

Number constants

  • Number.EPSILON: the smallest interval between two representable numbers
  • Number.MIN_SAFE_INTEGER: the smallest safe integer
  • Number.MAX_SAFE_INTEGER: the largest safe integer
  • Number.MAX_VALUE: the largest positive number JavaScript can represent
  • Number.MIN_VALUE: the smallest positive number JavaScript can represent
  • Number.POSITIVE_INFINITY: positive infinity (occurs on overflow)
  • Number.NEGATIVE_INFINITY: negative infinity (occurs on overflow)
  • Number.NaN: special value representing "not a number"
  • Number.NEGATIVE_INFINITY: special value representing negative infinity
  • Number.POSITIVE_INFINITY: special value representing positive infinity

Useful methods

  • isNaN: determines if a value is NaN
  • isFinite: determines if a value is a finite number
  • isInteger: determines if a value is an integer
  • isSafeInteger: determines if a value is an integer in the safe integer range
  • parseFloat: converts text to a float
  • parseInt: converts text to an integer, can specify radix
  • toExponential: returns a string representation in exponential form
  • toFixed: returns a string representation in fixed-point form
  • toPrecision: returns a string representation with the specified precision
  • toString: returns a string representation, can specify radix
  • toValue: returns the primitive value