WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript String Methods


String methods help you to work with strings.


Finding a String in a String

The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:

Example

var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");
Try it Yourself »

The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:

Example

var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate");
Try it Yourself »

Both the indexOf(), and the lastIndexOf() methods return -1 if the text is not found.

Both methods accept a second parameter as the starting position for the search.


Searching for a String in a String

The search() method searches a string for a specified value and returns the position of the match:

Example

var str = "Please locate where 'locate' occurs!";
var pos = str.search("locate");
Try it Yourself »


Extracting String Parts

There are 3 methods for extracting a part of a string:

  • slice(start, end)
  • substring(start, end)
  • substr(start, length)

The slice() Method

slice() extracts a part of a string and returns the extracted part in a new string.

The method takes 2 parameters: the starting index (position), and the ending index (position).

This example slices out a portion of a string from position 7 to position 13:

Example

var str = "Apple, Banana, Kiwi";
var res = str.slice(7,13);

The result of res will be:

Banana
Try it yourself »

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

This example slices out a portion of a string from position -12 to position -6:

Example

var str = "Apple, Banana, Kiwi";
var res = str.slice(-12,-6);

The result of res will be:

Banana
Try it Yourself »

If you omit the second parameter, the method will slice out the rest of the string:

Example

var res = str.slice(7);
Try it Yourself »

or, counting from the end:

Example

var res = str.slice(-12);
Try it Yourself »

The substring() Method

substring() is similar to slice().

The difference is that substring() cannot accept negative indexes.

Example

var str = "Apple, Banana, Kiwi";
var res = str.substring(7,13);

The result of res will be:

Banana
Try it yourself »

If you omit the second parameter, substring() will slice out the rest of the string.


The substr() Method

substr() is similar to slice().

The difference is that the second parameter specifies the length of the extracted part.

Example

var str = "Apple, Banana, Kiwi";
var res = str.substr(7,6);

The result of res will be:

Banana
Try it yourself »

If the first parameter is negative, the position counts from the end of the string.

The second parameter can not be negative, because it defines the length.

If you omit the second parameter, substr() will slice out the rest of the string.


Replacing String Content

The replace() method replaces a specified value with another value in a string:

Example

str = "Please visit Microsoft!";
var n = str.replace("Microsoft","welookups");
Try it Yourself »

The replace() method can also take a regular expression as the search value.

By default, the replace() function replaces only the first match. To replace all matches, use a regular expression with a g flag (for global match):

Example

str = "Please visit Microsoft!";
var n = str.replace(/Microsoft/g,"welookups");
Try it Yourself »

Converting to Upper and Lower Case

A string is changed over to capitalized with toUpperCase():

Example

var text1 = "Hello World!";      /String
var text2 = text1.toUpperCase();  //text2 is text1 changed over to upper
Try it Yourself »

A string is changed over to bring down case with toLowerCase():

Example

var text1 = "Hello World!";      /String
var text2 = text1.toLowerCase();  //text2 is text1 changed over to lower
Try it Yourself »

The concat() Method

concat() joins at least two strings:

Example

var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2);
Try it Yourself »

The concat() strategy can be utilized rather than the in addition to administrator. These two lines do the same:

Example

var content = "Hello" + " " + "World!";
var content = "Hello".concat(" ","World!");

Extracting String Characters

There are 2 safe strategies for separating string characters:

  • charAt(position)
  • charCodeAt(position)

The charAt() Method

The charAt() strategy restores the character at a predefined list (position) in a string:

Example

var str = "HELLO WORLD";
str.charAt(0);           /returns H
Try it Yourself »

The charCodeAt() Method

The charCodeAt() strategy restores the unicode of the character at a predefined list in a string:

Example

var str = "HELLO WORLD";

str.charCodeAt(0);        / returns 72
Try it Yourself »

Accessing a String as an Array is Unsafe

You may have seen code this way, getting to a string as an array:

var str = "HELLO WORLD";

str[0];                  /returns H

This is unsafe and unpredictable:

  • It does not work in all programs (not in IE5, IE6, IE7)
  • It makes strings look like exhibits (however they are not)
  • str[0] = "H" does not give a blunder (yet does not work)

If you need to peruse a string as an exhibit, convert it to a cluster first.


Converting a String to an Array

A string can be changed over to a cluster with the split() method:

Example

var txt = "a,b,c,d,e";   //String
txt.split(",");          //Split on commas
txt.split(" ");          //Split on spaces
txt.split("|");          //Split on pipe
Try it Yourself »

If the separator is precluded, the returned cluster will contain the entire string in file [0].

If the separator is "", the returned exhibit will be a variety of single characters:

Example

var txt = "Hello";       //String
txt.split("");           //Split in characters
Try it Yourself »