substring()

The substring() method is used to return the charcacters which are in the positions defined by numberOne and numberTwo (see syntax). The characters between the two numbers will be returned. Keep in mind that the values returned are zero based integers. That is, the first character of the string will be number 0 (zero), the next will be one, and so on. Also keep in mind that the character before numberTwo will be the last character returned. If numberOne is a negative number, it will be treated as a 0 (zero). If numberTwo is greater than the length of the string or a string.length setting, it will be treated as the string.length. If numberOne is equal to NumberTwo, an empty string will be returned (NOT null or undefined, simply empty).



It should be noted that in using this method with the LANGUAGE= attribute of the document set to version 1.2, there is a bug in early navigators that will throw an out of memory error if numberOne is greater than numberTwo. When this attribute is not set, the browser will return a string beginning with numberTwo and ending with numberOne.

syntax:

string.substring(numberOne, numberTwo)

string.substring(number)

EXAMPLE

var stringToSearch = new String("This is the string text");

var stringToSearch2 = stringToSearch.substring(5,12);

document.write("The returned string is : " + stringToSearch2)

The example first creates a new string object called stringToSearch. This string is then acted upon with the substring() method, returning the characters starting at position 5 and ending with the character before position 12. The contents of the search are placed in the stringToSearch2 string and written to the screen by a document.write statement. The output should look like the following:



The returned string is : is the