Strings

Constructor

String() - constructor call can be used to convert value to string

Static Methods

String.fromCodePoint(code) - get symbol from code

Methods

includes(string, from?) - check whether string includes another string and returns boolean

  • from - index of a character where to start search

startsWith()
endsWith()

indexOf(string, from?) - returns index of a character or a substring
lastIndexOf(string, from?)

  • both return -1 if no any matches were found


charAt(index) - returns a char, which is located at specific index
charCodeAt(index) - returns ASCII code of the char, which is located at specific index

localeCompare(string) - compare alphabetically

  • returns:
  • 1 - if first is higher
  • 0 - if equal
  • -1 - if first is lower
  • when using "<" & ">" for comparison, strings are compared using ASCII codes. each uppercase letter is lower than each lowercase letter


toString() - just returns string value

  • toString can be used on many other types, in order to get string representation of a value. check article of specific data type


split(separator, limit?) - splits a string and returns it


slice(start?, end?) - returns a part of the string

  • start - index of the first character, which is included
  • end - index of the first character, which is not included
  • start & end might be negative. in such case index is counted from the end of the string. indexing from the end is also zero-based
  • e.g.
  • slice(3) - from index 3 till the end of the string
  • slice(-3) - from index 3 from the end (not including) till the end of the string
  • slice(3, -3) - from index 3 to index 3 from the end (not including)


replace(pattern, replacement) - replaces part of the string and returns new string

  • pattern - string or regex. if string is used, only the first occurrence will be replaced. see [Regular Expressions] for more

replaceAll() - same as replace(), but replaces all occurrences

  • e.g. foo.replaceAll('_', '-')


trim() - remove spaces at the beginning and at the end
trimLeft()
trimRight()

toLowerCase()
toUpperCase()

  • tricks:
  • str[0].toUpperCase() + str.slice(1).toLowerCase() - capitalize word


repeat(n) - repeats the string n times
padStart(length, string?) - pad the current string with another string until the resulting string reaches the given length

  • string - default is space

all methods, which do string manipulations, leave original string unchanged

String Handling Terms

concatenation - combining two strings with the help of "+" sign

  • e.g. 'foo' + 'bar' - results in 'foobar'
  • string can also be combined with other type. e.g. 1 + '2' - results in '12'


interpolation - combining strings with dynamic values through template literal syntax

  • e.g. `str ${1 + 2}` - results in 'str 3'
  • dynamic data can be stored in variables or calculated on the fly
  • interpolation is a newer way of constructing dynamic strings, which introduces better readability


template literal - is a backticks syntax, which is alternative to quotes. it allows string interpolation and multi-line strings

  • \ - line continuation symbol. can be used to avoid new line


tagged template - allows to parse template literals with a function

  • e.g. getAge`${name} is ${age} years old`
  • getAge is a function which gets array of all strings as first parameter and all dynamic values as next parameters
  • might be known from styled-components library or Gatsby "graphql" function

Escape Sequences

\ - escape character. implements escaping in JS strings

  • e.g. \' - can be used to add apostrophe within the string with single quotes


\n - newline. helps to create multiline strings with single and double quotes
\t - tabulation
\r - carriage return

\b, \f, \v - backspace, form feed, vertical tab. these are coming from old times, not used nowadays

;