Javascript Lib

Useful String Methods and Properties
  • .length
  • .indexOf(str, [offset]) ES1
  • .search(str) ES3
  • .lastIndexOf(str) ES1
  • .includes(str) ES6
  • .startsWith(str) ES6
  • .endsWith(str) ES6
  • .slice(start, [end]) ES3
  • .substring(start, [indexEnd]) ES3
  • .substr(start, [length]) ES3
  • .replace(find, replace)
  • .charAt(number) ES1
  • .concat(str) ES3
  • .repeat(number) ES6
  • .match(regexp) ES3
  • .trim() ES5
  • .split(separator, [limit]) (sep is string or regex) ES3
  • .toLowerCase(str) ES1
  • .toUpperCase(str) ES1
  • .toString() ES3 (like ruby inspect, I think console.log calls this)
  • [@@iterator]()
// length not a method
log( 'str'.length )  3
 
// return character at any index
log( 'str' [ 1 ] )  t
 
// find location of substring
log( 'this is a string'.indexOf( ' is ' ) )  4
log( 'this is a string'.lastIndexOf( ' is ' ) )  4
 
// indexOf returns -1 if nothing found
log( 'this is a string'.indexOf( 'banana' ) )  -1
 
if ( 'this is a string'.indexOf( 'banana' ) === -1 ) {
  log( 'no bananas' )
 no bananas
 
// search is like indexOf but uses a regex (indexOf takes a start position, search doesnt)
// search forces a regex match so its slower.
log( 'this is a string'.search( ' is ' ) )  4
 
// slice returns a substring, isn't well named because it doesn't modufy
// existing string, second argument, the terminator (optional), isn't included,
// its "up to", if not specified will default to end of string.
let str = 'this-is-a-string'
let str2 = str.slice( 0, 4 )
log( str )  'this is a string
log(str2)  this
 
// with negative second argument it works like this
log( str.slice( 0, (str.length - 3) ) )  this-is-a-str
log( str.slice( 0, -3 ) )  this-is-a-str
 
// omit last character
log( str.slice( 0, -1 ) )  this-is-a-strin
 
// substring() seems to be a version of slice but with weird behavior, reorders first two parameters
 
// while slice uses indexes, substr uses index and length
log( str.substr( 0, 3 ) )  thi
 
// replace() finds and replaces a substring, takes regex, even /regex/g
log( 'one two three'.replace( 'two''TWO' ) )  one TWO three
 
// charAt seems like indexing
log( 'string'.charAt( 2 ) === 'string' [ 2 ] )  true
 
// concat two strings
log( 'hello '.concat( 'world' ) )
 
// three related es6 methods
log( 'one two three'.startsWith( 'one' ) )  true
// includes looks like indexOf() but without the ugly -1
log( 'one two three'.includes( 'two' ) )  true
log( 'one two three'.endsWith( 'three' ) )  true
 
// repeat, who cares
log( '*'.repeat( 5 ) )  *****
 
 
// split
log( 'abc'.split( ) )  ['abc'] does nothing
log( 'abc'.split( '' ) )  ['a''b''c'] separates on all characters
log( 'abc'.split( 'b' ) )  ['a''c'] separator is string
log( 'abc'.split( /b/ ) )  ['a''c'] separator is regex
log( 'abc'.split( /(b)/ ) )  ['a''b''c'] includes separator
 
let i = 'hello'[Symbol.iterator]()
log( i.next().value )  h
log( i.next().value )  e
log( i.next().value )  l
log( i.next().value )  l
log( i.next().value )  o