String Methods

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email
In JavaScript strings are stored as objects. Strings can be declared in two ways. The first one is by assigning the value within double quotes and the second one is by using the new keyword.

mystring ="Welcome to Pentasoft";
mystring = new String("Welcome to Pentasoft");
Both the above declarations are valid and there is absolutely no difference between the two.

Calculating the Length of the String

The length of a string can be calculated by using the length() method.

var mystring = "Pentasoft"
document.write(mystring. length)

The above statement produces a result of nine to be printed.The index of last character of the string is equal to the length of the string minus one.

charAt() method

This method is used to return a character by giving its index value as an argument. The characters of a string are indexed from zero to the length minus one.

var mystring Pentasoft
document.write(mystring.charAt(4))
The above statement writes the character "a" to the current document as it is the fifth character in the string.This method can also be called with a literal:

document.write(" Pentasoft".charAt(4))
The above version also produces the same result as before.

indexOf() method

This method returns the index f the first occurrence of the given substring in the calling string object:

var mystring = "Pentasoft"
document.write(str.indexOf("so"))

The above script produces an output of 5 to be printed.The first occurrence of the substring occurs at the sixth character of the substring.Therefore its index value 5 is printed.Another variation of the index0f method is to give the occurrence number of the substring.

var mystring = "baddba"
document.write(mystring.index0f("ba",2)

The above script writes the second occurrence of the string "ba" to the current document. The general syntax for the indexOf method is:

string Name. index0f (searchvalue, [occurrenceNum ber])

lastIndexOf() method

This method similar to the index0f 0 method except that the last occurrence of the specified string is returned.

substring()

The substring method can be used to return a portion of the main string. It takes in two parameters.The syntax for substring() method is:

stringName.substring(startlndex,endlndex+l)
The startIndex indicates the index of the first character in the substring and endlndex indicates the last index of the substring.

var mystring="pentasoft"
var newstring=mystring.substring(3,6)
The above statements assign tas to the variable newstring.

split()

The split method is used to separate a string based upon a specified delimiter. The following example illustrates the use of split:

var mystring = "Pentssoft learn from the leader"
var mysplit = mystring. split(" ")
The above script results in mystring being split based on the delimiter blankspace.The result is stored in an array mysplit.

toLowerCase() and toUpperCase()

As the names indicate the toUpperCaseo and toLowerCaseO methods are used to convert cases in alphabetic characters.

var s= " Pentasoft"
document.write(s.toUpperCase())
document.write(s.toLowerCase())
The above script writes PENTASOFT and pentasoft to the current document.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.