HTML and CSS Reference
In-Depth Information
Table 9.10
Methods for String Manipulation (continued)
Method
What It Does
search(regexp)
Searches for the regular expression and returns the index
of where it was found.
slice(startpos, endpos)
Returns string containing the part of the string from
startpos
to
endpos.
split(delimiter)
Splits a string into an array of words based on
delimiter.
substr(startpos, endpos)
Returns a subset of string starting at
startpos
up to, but
not including,
endpos.
toLocaleLowerCase()
Returns a copy of the string converted to lowercase.
toLocaleUpperCase()
Returns a copy of the string converted to uppercase.
toLowerCase()
Converts all characters in a string to lowercase letters.
toString()
Returns the same string as the source string.
toUpperCase()
Converts all characters in a string to uppercase letters.
valueOf
Returns the string value of the object.
Methods That Find a Position in a String.
A substring is a piece of an already
existing string; thus
eat
is a substring of both
create
and
upbeat
, and
Java
is a substring
of
JavaScript
. When a user enters information, you want to see if a certain pattern of
characters exist, such as the @ in an e-mail address or a zip code in an address. JavaScript
provides a number of methods to assist you in finding substrings.
The
indexOf()
and the
lastIndexOf()
methods are used to find the first instance or the
last instance of a substring within a larger string. They are both case sensitive. The first
character in a string is at index value 0, just like array indexes. If either of the methods
finds the substring, it returns the position of the first letter in the substring. If either
method can't find the pattern in the string, then a -1 is returned.
EXAMPLE
9.27
<html>
<head><title>Substrings</title></head>
<body bgcolor="lightgreen">
<font face="arial"
<big>
Searching for an @ sign
<script type="text/javascript">
1
var email_addr=prompt("What is your email address? ","");