Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

The value processed by a specific method is the accumulative value returned by the expression to the left of the method; that is, the expression that is implemented as the calling object, or string. Therefore, you must make sure that the expression to the left of a method returns a value which is valid for that method.

HTML text formatting tags usually consist of two tags that enclose the text. The nested structure is very clear in HTML, because a set of tags can enclose another set of tags. In the previous example, the <I></I> set encloses the <B></B> set. When creating HTML via JavaScript’s String methods, keep in mind that the far-left specified method appears as the inner set of tags when formatted to HTML, whereas the far-right method is responsible for the outer set of tags.

General String Methods

As you already know, the String object packs many methods. Those that convert strings to constant HTML formats (and listed above) are only some of the methods JavaScript offers. In this section, we shall take a look at the rest of the String methods.

charAt()

This method returns the character whose index is equal to the argument of the method. The characters of a string are indexed from 0 to length – 1. The general syntax is:

anyString.charAt(index)

Here is an example:

var pres = "Kennedy"
document.write(pres.charAt(1))

This script segment prints the character “e”, because it is the second character in the string. You can also call this method with a literal as in the following example:

document.write("Kennedy".charAt(1))

You can print the characters of a string via a simple loop:

var str = "I am a string!"
for (var i = 0; i < str.length; ++i) {
 document.write(str.charAt(i))
}

At first, a string literal is assigned to the variable str. The loop then iterates length times. It starts at 0, and ends at str.length – 1 (notice the less than operator, not less than or equal to). The i-indexed character is printed in the ith iteration of the loop. Since the command block is executed once for each integer value of i, each character of the string is printed once and only once. The output of the preceding script segment is actually the string itself, printed one character at a time (no, you cannot notice the difference!).

indexOf()

This method returns the index of the first occurrence of the specified substring in the calling string object, starting the search at the beginning of the string. An example will surely clear things up:

var str = "ababa"
document.write(str.indexOf("ba"))

This script’s output is the number 1. The first occurrence of the substring “ba” in the calling string object is at the second character whose index is 1. The search for the specified substring starts at index 0, the beginning of the string. However, you can also instruct JavaScript to start the search at a different index, somewhere in the middle of the string. The following script segment prints the number 3:

var str = "ababa"
document.write(str.indexOf("ba", 2))

The general syntax of this method is:

stringName.indexOf(searchValue, [fromIndex])

Note that fromIndex must be an integer between 0 and the string’s length minus 1. SearchValue does not have to be a string. The following script prints 8:

var str = "August 27, 1985"
document.write(str.indexOf(7))

If the index to start looking for the substring is not specified, the default value is used, 0.

If the specified substring is not found in the entire string, the method returns one less than the base, –1. This method is equivalent to the index function in Perl.

lastIndexOf()

This method is identical to the indexOf method, except that it returns the index of the last occurrence of the specified value, rather than the first occurrence. Its syntax is, therefore, the same:

stringName.lastIndexOf(searchValue, [fromIndex])

The following script prints the number 3:

var str = "a/b/c"
document.write(str.lastIndexOf("/"))

See the indexOf method for more details on this method.

substring()

Strings are constructed of characters. The substring() method returns a set of characters within its calling String object. Its general syntax is:

stringName.substring(indexA, indexB)

stringName is any string. indexA and indexB are both integers between 0 and stringName.length – 1. indexA is the index of the first character in the substring, whereas indexB is the index of the last character in the substring plus 1. The following script assigns the string “bc” to the variable seg:

var str = "abcd"
var seg = str.substring(1, 3)

Notice that the length of the substring is indexA – indexB.

The substring whose arguments are 0 and stringName.length is equal to the string itself (stringName).

This method is similar to its equivalent in Perl, the function substr. Nonetheless, it is important to point out the differences. First of all, since Perl does not support objects, the plain substr() function accepts the string itself as the first argument and indexA as the second one. However, the third argument is not indexB, but the length of the substring. Another difference is that when you call the function with a negative value as the offset (indexA), the substring starts that far from the end of the string. In JavaScript, though, a negative index is equivalent to a zero index, the first character of the string. JavaScript prototypes enable us to reproduce the substr function in Perl as a JavaScript method using the following script segment:

function substr(offset, length) {
 if (offset < 0)
  offset = this.length + offset
 return this.substring(offset, offset + length)
}
String.prototype.substr = substr

You can use this method with any string in the following way:

var str = "abcd"
document.write(str.substr(–3, 2))

This statement prints the string “bc”.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us