• Aucun résultat trouvé

Finding Your Way Around a String

You know that a string is an array of characters. You recall from Chapter 4 that an array has one or more elements that are identifi ed by an index. Each character in a string is an array element that can be identifi ed by its index. Take a look at the following example to see how this works:

var FirstName = 'Bob'

You recognize that 'Bob' is a string that is assigned to the variable FirstName.

This variable is actually an array. The fi rst element of the array has the value B. The second element has the value o and the last element has the value b. Remember that the index of the fi rst element is 0 and not 1. Therefore, you use the index 0 to refer-ence B and 1 to referrefer-ence o and 2 to referrefer-ence b.

You can copy a character from a string to another string by using the charAt() method of the string object. The charAt() method requires one argument, which Figure 6-2 You must place a space as the last character in the string 'Bob' to make sure the space appears between the fi rst and last names in the concatenated string.

ch06.indd 120

ch06.indd 120 5/2/2005 3:51:06 PM5/2/2005 3:51:06 PM

is the index of the character that you want to copy. The following statement illus-trates how to use the charAt() method:

var SingleCharacter = NameOfStringObject.charAt(index)

The next example shows how to display the fi rst character of the string 'Bob' by calling the charAt() method (Figure 6-3).

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Copy one character of a string</title>

</head>

<body>

<script language="Javascript" type="text/javascript">

var FirstName = 'Bob'

var Character = FirstName.charAt(0) alert(Character)

-->

</script>

</body>

</html>

Sometimes you won’t know the index of the character you need because the string is supplied to your JavaScript when the script runs. This occurs, for example, when the person who runs your JavaScript enters the string. You can determine the index of a character by calling the indexOf() method of the string object. The indexOf() method returns the index of the character passed to it as an argument.

If the character is not in the string, the method returns –1. You should usually check for this when executing this function. Here’s how to use this method:

var IndexValue = string.indexOf('character')

This next example calls the indexOf() method to return the index of the sec-ond letter of 'Bob'. The index is then passed to the charAt() method to copy

Figure 6-3 The fi rst letter of the string is returned by the charAt() method.

ch06.indd 121

ch06.indd 121 5/2/2005 3:51:06 PM5/2/2005 3:51:06 PM

the o to a variable. Both the index (Figure 6-4) and the character are then displayed on the screen (Figure 6-5).

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Identifying the index of a character in a string</title>

</head>

<body>

<script language="Javascript" type="text/javascript">

var FirstName = 'Bob'

var IndexValue = FirstName.indexOf('o') alert(IndexValue)

if(IndexValue != -1) {

var Character = FirstName.charAt(IndexValue) alert(Character)

} -->

</script>

</body>

</html>

NOTE

Figure 6-4 The index is retrieved by calling the indexOf() method.

Figure 6-5 The character is retrieved by calling the charAt() method.

ch06.indd 122

ch06.indd 122 5/2/2005 3:51:07 PM5/2/2005 3:51:07 PM

If you do not know the character you want (for example, if it is determined by user input), but you know the position of the character relative to the end of the string, you can use the length value of the string object to calculate the position of the character. Here’s how:

var LengthOfString = string.length

Here’s a practical application: Suppose you wanted to use the last four digits of a person’s Social Security number for the person’s ID. You can copy these digits to a new string, but you need to know the index of the fi rst of the four digits. You’ll learn how to copy this in the “Copying a Substring” section later in this chapter. For now, let’s see how we can use the length value of the string to identify the index of the fi rst of the last four digits.

NOTE

NOTE The length value contains the number of characters in the string—but don’t confuse this with the index of the last character. Remember that the index begins with 0 and not 1, so the index of the last character of the string is length – 1.

If the value of length is 3, you know that the string has three characters. On the other hand, the index of the last character is 2.

We can use the length value to calculate the index of the character that we want to use. Here how this is done:

var SSNumber = '123-45-6789'

var IndexOfCharacter = SSNumber.length - 4

The length of the SSNumber is 11. Since we want the fi rst of the last four numbers, we subtract 4 from the length, and this gives us 7. This means that the fi rst of the last four numbers is at index 7 in the string.

A Social Security number is just one of many types of standardized formatted text. As long as you know the format of the text, you can use the length value to calculate the index of a character within the text.