× Introduction Setup Environment Building blocks Built-in functions Data Types Strings Operators Conditional statements Loop statements Functions Arrays Understaing Objects Date Object Number Object Math Object String Object Window Location Navigator History DOM Basics Forms
   Programs
Basic Control Loops Functions Arrays Examples Projects Quick Ref.
   Exercises
Variables Data Types Operators Decision Loops Reeborg's World



JavaScript String object

In this tutorial you will learn how to work with String Object.

The String object provides properties and methods to get information about strings or to modify strings. A String object is created in either of two ways: a programmer creates one by using the new keyword with the constructor function, or JavaScript creates one temporarily when one of the methods is called from a string literal.

The String Object

As just explained, one way to create a String object is to use the new keyword, as you’ve done with other objects previously. The syntax is shown here:

Example
var guitar_string = new String("G");

The String Literal

You can create a string literal just by assigning a string value to a variable. This technique is a bit shorter than creating a String object using the new keyword and still allows you to use all the methods of the String object (as well as one of the properties).

A string literal is created in the code that follows. Notice that the code assigns a string value to a variable.

Example
var guitar_string = "G";

This makes the string “G” a string literal, which you know as a regular text string. With text strings, you’re also allowed to use the properties and methods of the String object.

What’s the Difference?

The difference between a String object and a string literal is that a regular text string has the value of the string itself, and it can be compared against another string easily. Objects aren’t going to be equal to one another in the same way regular text strings would be. To find out if two objects are equal, you would have to write extra code to determine that.

Properties of the String Object

The length Property

This property returns the length of the string, which is the number of characters contained in the string.

Example
var myname="Krishna";
document.write("The name has "+myname.length+" characters.");

Methods of the String Object

Here is a list of the methods available in String object along with their description.

  • charAt() Returns the character at the specified index.
  • charCodeAt() Returns a number indicating the Unicode value of the character at the given index.
  • concat() Combines the text of two strings and returns a new string.
  • indexOf() Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
  • lastIndexOf() Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
  • localeCompare() Returns a number indicating whether a reference string comes before or after or is the same as the given string in sorted order.
  • match() Used to match a regular expression against a string.
  • replace() Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
  • search() Executes the search for a match between a regular expression and a specified string.
  • slice() Extracts a section of a string and returns a new string.
  • split() Splits a String object into an array of strings by separating the string into substrings.
  • substr() Returns the characters in a string beginning at the specified location through the specified number of characters.
  • substring() Returns the characters in a string between two indexes into the string.
  • toLocaleLowerCase() The characters within a string are converted to lower case while respecting the current locale.
  • toLocaleUpperCase() The characters within a string are converted to upper case while respecting the current locale.
  • toLowerCase() Returns the calling string value converted to lower case.
  • toString() Returns a string representing the specified object.
  • toUpperCase() Returns the calling string value converted to uppercase.
  • valueOf() Returns the primitive value of the specified object.