Skip to main content

Javascript: String Methods

.indexOf

If you are looking to find a certain character or set of characters in a string you can use the .indexOf method to return the position of the character in integer format. For example

var myName = "My Name is Nick Coughlin"

myName.indexOf("N")

//11

Will return 11 because that is the position in the string of the letter N which is what we were searching for. You can also search for a set of character such as is.

var myName = "My Name is Nick Coughlin"

myName.indexOf("is")

//8

You can also use this property to check whether a character or string of characters is inside of a string using this method. If the character or string is not inside of the string the return will be -1

var myName = "My Name is Nick Coughlin"

myName.indexOf("Max")

//-1

And therefore we can simply check to see if it is true that the .indexOf is ===-1. If it is then the character you are searching for is not present.

.length

This will return the length of the string. For example:

myname.length is 15

.charAt()

This will select the character at whatever index position you specify in the parentheses:

the letter m

.toUpperCase()

Will capitalize a string.

whole string is capitalized

This can be combined with .charAt to select and capitalize a specific letter. For example:

the letter n is capitalized

.slice()

This property will return the string starting from the index which is chosen. Essentially slicing off the characters at the front.

2 letters removed from name

.replace

Replace a character, or set of characters in a string with another character.

string.replace(searchvalue, newvalue)

Comments

Recent Work

Free desktop AI Chat client, designed for developers and businesses. Unlocks advanced model settings only available in the API. Includes quality of life features like custom syntax highlighting.

Learn More

BidBear

bidbear.io

Bidbear is a report automation tool. It downloads Amazon Seller and Advertising reports, daily, to a private database. It then merges and formats the data into beautiful, on demand, exportable performance reports.

Learn More