JavaScript - String Methods

Strings have a length property and characters can be accessed by zero-based index.

var msg = "Hello there"; console.log(msg.length); // 11 console.log(msg[6]); // t

Selected Methods

Method Description
toUpperCase Returns a new string with all characters in upper case.
toLowerCase Returns a new string with all characters in lower case.
indexOf Returns the index of the first character in the first occurrence of a search string within a target string.
lastIndexOf Returns the index of the first character in the last occurrence of a search string within a target string.
replace Returns a new string where one or more occurrences of a search string have been replaced with a specified replacement string.
split Returns an array of strings by splitting the target string into pieces. Where the splits occur are determined by a specified separator.
substr Returns part of a target string, starting at a specified index and with a specified number of characters.
substring Returns part of a target string, starting at a specified index and stopping at a specified index.
trim Returns a new string with leading and trailing whitespace removed.

toUpperCase

The toUpperCase method returns a new string with all characters in upper case.

var msg = "Hello There"; console.log(msg.toUpperCase()); // HELLO THERE

toLowerCase

The toLowerCase method returns a new string with all characters in lower case.

var msg = "Hello There"; console.log(msg.toLowerCase()); // hello there

indexOf

The indexOf method returns the index of the first character in the first occurrence of a search string within a target string. Pass a second argument to specify an index at which to start searching. If the search string can't be found then the method returns -1.

var msg = "Hello There"; console.log(msg.indexOf('e')); // 1 console.log(msg.indexOf('e', 2)); // 8 console.log(msg.indexOf('he')); // 7 console.log(msg.indexOf('He')); // 0 console.log(msg.indexOf('hello')); // -1

lastIndexOf

The lastIndexOf method returns the index of the first character in the last occurrence of a search string within a target string. Pass a second argument to specify an index at which to start searching backwards. If the search string can't be found then the method returns -1.

var msg = "Hello There"; console.log(msg.lastIndexOf('e')); // 10 console.log(msg.lastIndexOf('e', 2)); // 1 console.log(msg.lastIndexOf('he')); // 7 console.log(msg.lastIndexOf('He')); // 0 console.log(msg.lastIndexOf('hello')); // -1

replace

The replace method returns a new string where one or more occurrences of a search string have been replaced with a specified replacement string.

var msg = "Hello There"; console.log(msg.replace('There', 'Jason')); // Hello Jason

When using a search string (rather than a regular expression), only the first match is replaced.

var msg = "Hello There"; console.log(msg); // Hello There msg = msg.replace('e', 'zzz'); console.log(msg); // Hzzzllo There msg = msg.replace('e', 'zzz'); console.log(msg); // Hzzzllo Thzzzre

You can use regular expressions to specify search criteria.

var msg = "Hello There"; console.log(msg); // Hello There msg = msg.replace(/e/g, 'zzz'); console.log(msg); // Hzzzllo Thzzzrzzz

split

The split method returns an array of strings by splitting the target string into pieces. Where the splits occur are determined by a specified separator.

var msg = "red, blue, green, yellow"; var bits = msg.split(', '); console.log(bits); // ["red", "blue", "green", "yellow"]

Passing an empty string as the separator returns an array of the characters in the target string.

var msg = "Red, blue"; var bits = msg.split(''); console.log(bits); // ["R", "e", "d", ",", " ", "b", "l", "u", "e"]

substr

The substr method returns part of a target string, starting at a specified index and with a specified number of characters.

var msg = "Over the moon!"; console.log(msg.substr(9, 3)); // moo

substring

The substring method returns part of a target string, starting at a specified index and stopping at a specified index.

var msg = "Over the moon!"; console.log(msg.substring(9, 12)); // moo

trim

The trim method returns a new string with leading and trailing whitespace removed.

var msg = " Mmm, breeeezy "; msg = msg.trim(); console.log(msg); // "Mmm, breeeezy"

trim is a relatively new JavaScript method and is implemented in newer browsers. It won't work in IE8 and earlier.

Further Help

You can find an excellent reference of properties and methods on the Mozilla Developer Network String page.