JavaScript String Object
In this JavaScript tutorial, you will learn about String Object, purpose of string object in JavaScript, purpose of string object, indexof method, lastIndexOf method and substring method along with syntax and example.
Purpose of String Object in JavaScript:
The main purpose of String Object in JavaScript is for storing text.
General method of using String Object is to declare a variable and assign a string, in other words a text to the variable.
var exf="Welcome"
assigns the text Welcome to the variable exf defined.
We have seen in earlier section that some of the methods used in String Objects are:
- indexOf
- lastIndexOf
- substring()
- toUpperCase()
- toLowerCase()
indexOf method of String Object:
If a programmer wants to know the position of a characters or group of characters in a String Object it can be obtained by applying the indexOf method to the String. In other words indexOf method of the String Object returns the position of the first occurrence of a specified string value in a string.
General syntax of the indexOf method of String Object in JavaScript is as follows:
indexOf(substr, [start])
Here the substr given in argument is searched in the string and if it found the index number of the searched character is or substring within the string is returned. If it is not found then -1 is returned. The default returned value is 0. The start is an optional argument specifying the position within string to begin the search.
For example:
|
Output of the above program is:
0
-1
This is because the first write statement has Welcome given as substring in the indexOf method and since it occurs in the starting of the String Object the value 0 is printed. The second write statement has Members given as substring in the indexOf method and since it does not occur in the string the value -1 is printed. Only members is present in the String Object and not Members so -1 is printed.
lastIndexOf method of String Object:
General syntax of lastIndexOf method of String Object is as follows:
lastIndexOf(substr, [start])
In this the substring given in parameters is searched for in the String and if it is found the index number of the searched character or substring within the string is returned. Here the order of search is from end to beginning of the String. If it is not found the value returned is -1. Start is an optional argument specifying the position within string to begin the search. Default is string.length-1.
For example
"exfor".lastIndexOf("o")
will give a value of 3. This is because the position of text o appearing in the string is counted from the end of string and the end of string is equal to stringlenth -1 which is equal to 4 and so position of o appearing in the string is 3.
substring method of String Object:
General syntax of substring method of String Object in JavaScript is as follows:
substring(from, [to])
In this method characters in the string between from and to indexes is returned "to" is optional, and if omitted, characters up to the end of the string is returned.
For example:
|
Output of the above program is
Wel
ome
This is because the first substring extracts text starting from the first till the second character which gives Wel as output and the second substring extracts text starting from the fourth till the end of string as the optional to is not specified.
We have seen only some of the methods of the String Object above. Apart from these, there are many other methods available for String Object like split, slice, charAt, concat, replace.