Here are some cheat sheets for common Javascript string needs. Your choice of built-in methods or regular expressions.

This page covers the following:

  • Removing the Last Character of a String
  • Removing the First Character of a String
  • Making a String Uppercase
  • Making a String Lowercase
  • Making the First Character of a String Uppercase
  • Get the Length of a String (Character Count)
  • Combining (Concatenating) Strings
  • Find the First Occurrence of a String or Character
  • Find the Last Occurrence of a String or Character
  • Count the Occurrences of a String or Character
  • Check the Character at a Certain Position
  • Check Existence of a Character or String Inside a String
  • Split Up a String

Solid Tip: The first two items compare the efficiency of substr(), substring(), replace(), and slice() running one million operations on a Core i7 CPU and Firefox 3.6.3. The tested string was 20 words. While regular expressions are often the fastest (and most difficult) way of manipulating strings, they are the slowest for these two specific tasks. This means the replace() method with regexp should not be used to remove the first or last character of a string. The fastest method for these first two is actually slice. It is about 25% faster than the substr() or substring() methods. However, due to their versatility, I would stick with substr() or substring(). View this post to see the difference in those two.;

Remove the Last Character of a String (4 choices)

JAVASCRIPTview code
// slice - fastest 0.00072 milliseconds
var newstr = str.slice(0,-1);
 
// substring -fast 0.00092 milliseconds
var newstr = str.substring(0, str.length-1);
 
// substr - fast 0.00092 milliseconds
var newstr = str.substr(0,str.length-1);
 
// regexp - slowest 0.00360 milliseconds
var newstr = str.replace(/.$/,'');

Remove the First Character of a String (5 choices)

JAVASCRIPTview code
// substring -fast 0.00092 milliseconds
var newstr = str.substring(1, str.length);
 
// substr -fast 0.00094 milliseconds
var newstr = str.substr(1,str.length-1);
 
// substr lazy -fast 0.00092 milliseconds
var newstr = str.substr(1,str.length);
 
// regexp -slowest 0.00180 milliseconds
var newstr = str.replace(/^./,'');
 
// slice - fastest 0.00072 milliseconds
var newstr = str.slice(1);

Make a String Uppercase

JAVASCRIPTview code
var newstr = str.toUpperCase();

Make a String Lowercase

JAVASCRIPTview code
var newstr = str.toLowerCase();

Make First Character of a String Uppercase

JAVASCRIPTview code
var newstr = str.substr(0,1).toUpperCase()+str.substr(1);

Get Length of a String (Character Count)

JAVASCRIPTview code
var count = str.length;

Combining (Concatenating) Strings

JAVASCRIPTview code
var newstr= str+str2;

Find First Occurrence of a String or Phrase

JAVASCRIPTview code
var str = 'hello world';
// indexOf
var pointer = str.indexOf('wor');
// search
var pointer = str.search('wor');
// search regexp
var pointer = str.search(/wor/);
 
// pointer is equal to 6
// first character counts as 0

Find Last Occurrence of a String or Phrase

JAVASCRIPTview code
var str = 'hello world';
var pointer = str.lastIndexOf('o');
 
// results in 7
// first character counts as 0

Count Occurrences of a String or Phrase

JAVASCRIPTview code
// count occurences of the letter 'o', will result in 5
 
var str = 'hello world, lets count some more';
// split
var count = str.split('o').length - 1;
// match
var count = str.match(/o/g).length;

Check the Character at a Certain Position

JAVASCRIPTview code
var str = 'hello world';
var char = str.charAt(6);
 
// results in 'w'
// first character counts as 0

Check Existence of a Character or Phrase Inside a String

JAVASCRIPTview code
var str = 'hello new world';
// search
var found = str.search('new');
// found will equal 6, would return -1 if not found
 
// regexp exec()
// don't bother with this method
var found = /new/;
found.exec(str);
// will return ['new'], would return null if not found
 
// regexp test()
// shortcut to exec() != null
var found = /new/;
found.test(str);
// will return true, would return false if not found

Split Up a String

JAVASCRIPTview code
var str = 'hello-world';
var group = str.split('-');
 
// results in array with 2 parts
// group[0] = 'hello';
// group[1] = 'world';