All string methods are immutable, meaning they cannot be changed only replaced.

str.length

returns the length of the string, chararacters.

var str = 'abcdefghijklmnopqrstuvwxyz';
str.length;
// ==> 26

str.indexOf(xxx)

returns the position of the first occurance of the arg passed into it. returns -1 if no occurances are found.

if we get the length of the string it will return the value, counting the first character as 1, but javascript counts strings by counting the first character as 0, the second char as 1 and so on.

var str = 'The new car is cool.';
str.indexOf('new');
// ==> 4

str.indexOf('c');
// ==> 8

str.lastIndexOf(xxx)

return the position of the last occurance of the arg passed into it. returns -1 if no occurances are found.

var str = 'The new car is the new cool.';
str.lastIndexOf('new');
// ==> 19

str.search(xxx)

does basically the same thing as .indexOf(). The difference being that search() can take more powerful search values such as regex’s.

var str = 'The new car is the new cool.';
str.search('new');
// ==> 4

str.slice(start, end)

extracts part of a string & returns the extracted value. .slice() takes 2 parameters, the position in the string where you want to start slicing, and the position you want it to stop slicing. does not alter the string the method is being called on.

var str = 'The new car is cool.';
str.slice(8, 11);
// ==> 'car'

var index = str.indexOf('new'); // 4
str.slice(index, 7);
// ==> 'new'

if a parameter is negative, the position is counted from the end of a string.

var str = 'the new car is cool.';
str.slice(4, -1);
// ==> 'new car is cool'

str.slice(4, -3)
// ==> 'new car is co'

if you only pass in 1 parameter, it will slice the array starting at the passed in param and ending at the end of the string.

var str = 'the new car is cool.';
str.slice(4);
// ==> 'new car is cool.'

str.slice(-7)
// ==> 's cool.'

negative position do not work on Internet Explorer version 8 and earlier.

str.substring(start, end)

basically the same as .slice(), but does not accept negative values.

var str = 'the new car is cool.';
str.substring(4, 7);
// ==> 'new'

str.substring(4);
// ==> 'new car is cool.'

str.substr(start, length)

also similar to .slice(). difference is that the second parameter of .substr specifies the length of the extraction, whereas .slice()’s second param is the position.

var str = 'The new car is cool.';
str.substr(4, 3);
// ==> 'new'

if the first param is negative, it counts from the end of the string. the last param can’t be negative because it specifies the length. if you omit the second param all together it will just slice out the rest of the string.

str.replace(xxx, yyy)

the .replace() method takes 2 arguments, the first being a string for what to replace, and the second a string for what to replace it with. it does not alter the original string it is called on, instead it returns a new string.

var str = 'The new car is cool.';
str.replace('car', 'bike');
// ==> str = 'The new bike is cool.'

.replace() can also take in a regex as the search value.

by default, .replace() only replaces the first instance. to make it replace all instances, pass in a regex with the ‘g’ flag (global).

var str = 'The new car is a car and it is cool.';
str.replace(/car/g, 'bike');
// ==> 'The new bike is a bike and it is cool.'

str.toUpperCase(xxx)

converts all characters in a string to be converted to uppercase. does not alter the original string, returns a new one.

var str = 'The new car is cool.';
str.toUpperCase();
// ==>

str.toLowerCase(xxx)

converts all characters in a string to be converted to uppercase. does not alter the original string, returns a new one.

var str = 'The new Car is Cool.';
str.toLowerCase();
// ==> 'the new car is cool.'

str.concat(xxx, yyy)

joins together 2 or more strings

var str1 = 'Hello';
var str2 = 'World!';
var str3 = str1.concat(' ', str2);
// ==> Hello World!

str.chatAt(xxx)

returns the character at the specified index in a string.

var str = 'The new car is cool';
str.charAt(10);
// ==> 'r'

str.chatCodeAt(xxx)

returns the unicode of the character at the specified index in a string.

var str = 'The new car is cool.';
str.charCodeAt(10);
// ==> 114

str.split(xxx)

converts strings to an array, splitting the string up based of redundant characters or patterns within the array.

var str = 'The new car is cool, fun, fast and sleek';
str.split(',');
// ==> [ 'The new car is cool', ' fun', ' fast and sleek' ]

str.split(' ');
// ==> [ 'The', 'new', 'car', 'is', 'cool,', 'fun,', 'fast', 'and', 'sleek' ]