Strings

The String is one the most used objects in JavaScript, which is why it is so important to know its properties and methods. To clear up some confusion, you already know this object as a string variable. As I just explained in the previous tutorial, this variable was created by a string() function. So, this variable is actually an object with properties and methods.

The string is a pretty large class because developers want to do some many different things with strings. The idea of a string in itself is very complicated. You might want to know how many “e” characters are in it, but you might also want to know the total number of characters in the string. Those are just two common reasons why we have properties. JavaScript does a really good job of providing us with a wealth of properties to get what we need from our strings.

Common String Properties

constructor – tells us the function that created the String object’s prototype

Example

Result

function String() { [native code] }

length – returns the total number of characters in a string

Example

Result

7

Common String Methods

A string is a very changeable variable, which means you need to be careful when using string methods. Many of the string methods will manipulate the initial value of the string, which cannot be recovered. You might have noticed already, but methods are easily distinguished by the fact they have () following them.

charAt() – returns character using its index

Example

Result

M

indexOf() – returns the index of the first instance of a specified value

Example

Result

1

lastIndexOf() – returns the index of the last instance of a specified value

Example

Result

2

match() – finds all matches in the string of a specified value

Example

Result

M,m

replace() – finds all matches in the string of a specified value and then replaces them with a new substring

Example

Result

Moorkat

search() – looks for a match in the string with a specified substring and returns the substrings index in the string

Example

Result

5

slice() – extracts and returns part of the string

Example

Result

kat

split() – splits string into an array of substrings

Example

Result

M,e,e,r,k,a,t

substr() – extracts characters from a string starting at a specified position and ending at a specified length from the starting position

Example

Result

erk

substring() – extracts characters from a string starting between two specified positions

Example

Result

r

toLowerCase() – converts all letters in string into lowercase

Example

Result

meerkat

toUpperCase() – converts all letters in string into uppercase

Example

Result

MEERKAT
If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.