String methods in JavaScript

Examples of inbuilt string methods, or functions, that are used for manipulating string data.

a bored fish

A to Z of string methods

atpadEndstartsWith
charAtpadStartsubstring
charCodeAtrepeattoLowerCase
codePointAtreplacetoUpperCase
endsWithreplaceAlltrim
indexOfslicetrimEnd
lastIndexOfsplittrimStart
length

String object

A string object represents a sequence of characters that can be manipulated.

const name = 'Michael';

const name_2 = new String('Michael');

↑ A to Z of methods

.at()

To get the character at a postion in the string (zero based counting) or get the character at a reverse postion in the string (not zero based).

const name = 'Michael';

name.at(3); // returns h

name.at(-2); // returns e

↑ A to Z of methods

.charAt()

To get the character at a postion in the string (zero based counting). This method does not work in the reverse sense.

const name = 'Michael';

name.charAt(3); // returns h

↑ A to Z of methods

.charCodeAt()

To get the character code (UTF 16) at a postion in the string (zero based counting). This does not work in the reverse sense.

const name = 'Michael';

name.charCodeAt(4); // returns 97

For more information on UTF 16 characters, here is a useful web based Encoding Problem Table from ‘string functions’.


↑ A to Z of methods

.charPointAt()

To get the code point value as a number (UTF 16) at a postion in the string.

const name = 'Michael';

name.codePointAt(3); // returns 104

↑ A to Z of methods

.concat()

Join two or more strings together with concat.

const name = 'Mike';

name.concat(' ', 'is awesome'); // returns 'Mike is awesome'

↑ A to Z of methods

.endsWith()

A boolean is returned.

const name = 'Michael';

name.endsWith('c'); // returns false
name.endsWith('l'); // returns true

↑ A to Z of methods

.includes()

Does the string contain the case-sensitive character? A boolean is returned.

const name = 'Michael';

name.includes('m'); // returns false
name.includes('M'); // returns true

↑ A to Z of methods

.indexOf()

Find the position of the first matching character. If there is no match -1 is returned.

const river = 'Mississippi';

river.indexOf('s'); // returns 2
river.indexOf('p'); // returns 8

river.lastIndexOf('w'); // returns -1

↑ A to Z of methods

.lastIndexOf()

Returns the position of the last matching character. If there is no match -1 is returned.

const river = 'Mississippi';

river.lastIndexOf('s'); // returns 6
river.lastIndexOf('p'); // returns 9

river.lastIndexOf('w'); // returns -1

↑ A to Z of methods

string length

Count the number of characters in a string with .length

const saying = 'I spy with my little eye';

saying.length; // returns 24

↑ A to Z of methods

.padEnd()

Add ‘padding’ to the end of a string with a given number of characters.

const developer = 'My name is Mike';

developer.padEnd(20, '.'); // returns 'My name is Mike.....'

↑ A to Z of methods

.padStart()

Adds ‘padding’ to the beginning of a string to a given number of characters.

const developer = 'My name is Mike';

developer.padStart(20, '.'); // returns '.....My name is Mike'

↑ A to Z of methods

.repeat()

Return a new string which containing the specified number of copies of the orginal string.

const original = 'Happy birthday to you, ';

original.repeat(2); // returns 'Happy birthday to you, Happy birthday to you, '

↑ A to Z of methods

.replace()

Replaces the first instance of the specified word.

const developer = 'My name is Mike. Mike is awesome';

developer.replace('Mike', 'Michael'); // returns 'My name is Michael. Mike is awesome'

↑ A to Z of methods

.replaceAll()

Replaces the all instances of the specified word.

const dev = 'My name is Mike. Mike is awesome';

dev.replaceAll('Mike', 'Michael'); // returns 'My name is Michael. Michael is awesome'

↑ A to Z of methods

.slice()

Extract characters from a string with slice.

const dev = 'My name is Mike. Mike is awesome';

dev.slice(-7); // returns 'awesome'

dev.slice(17); // returns 'Mike is awesome'

dev.slice(11, 21); // returns 'Mike. Mike'

↑ A to Z of methods

.split()

Create an array of the string characters

const name = 'Michael';

name.split(); // returns ['Michael']

name.split(''); // returns ['M', 'i', 'c', 'h', 'a', 'e', 'l']

↑ A to Z of methods

.startsWith()

A boolean returned on a case-sensitive match or otherwise of first character

const name = 'Michael';

name.startsWith('S'); // returns false
name.startsWith('M'); // returns true

↑ A to Z of methods

.substring()

A new string is made from a selection of the original characters.

const dev = 'My name is Mike. Mike is awesome';

dev.substring(17); // returns 'Mike is awesome'

dev.substring(3, 10); // returns 'name is'

↑ A to Z of methods

.toLowerCase()

Turns all characters to small case.

const name = 'Mike';

name.toLowerCase(); // returns 'mike'

↑ A to Z of methods

.toUpperCase()

Turns characters to capitalised case.

const name = 'Mike';

name.toUpperCase(); // returns 'MIKE'

↑ A to Z of methods

.trim()

Remove white space at both ends of the string.

const name = ' Mike ';

name.trim(); // returns 'Mike'

↑ A to Z of methods

.trimEnd()

Remove white space at the end of the string.

const name = ' Mike ';

name.trimEnd(); // returns ' Mike'

↑ A to Z of methods

.trimStart()

Remove white space at the beginning of the string.

const name = ' Mike ';

name.trimStart(); // returns 'Mike '

↑ A to Z of methods

Thank you for stumbling across this website and for reading my blog post entitled String methods in JavaScript