HTML and CSS Reference
In-Depth Information
document.write("The search() method found \"dad\" at
position "+ index +"<br />");
var mysubstr=straddr.substr(index,3) ;
document.write("After replacing \"dad\" with \"POP\" <br />");
document.write( straddr.replace(mysubstr,"POP") +"<br />");
An e-mail address is assigned to the string variable straddr .
The replace() method takes two arguments, the search string and the replacement
string. If the substring Daniel is found, it is replaced with Jake .
The search() method takes a substring as its argument and returns the first posi-
tion where a substring is found in a string. In this example the substring dad is
searched for in the string and is found at position 13.
The substr() method returns the substring found at position 13, 3 in the string, : dad.
The substring dad is replaced with POP in the string (see Figure 9.35).
Figure 9.35 The search() and replace() String methods: Output from Example 9.30.
9.5.5 The Number Object
Now that we've travelled this far in JavaScript, have you wondered how to format a float-
ing-point number when you display it, as you can with the printf function in C or Perl?
Well, the Number object, like the String object, gives you properties and methods to handle
and customize numeric data. The Number object is a wrapper for the primitive numeric
values (see Chapter 2, “Script Setup”), which means you can use a primitive number type
Search WWH ::

Custom Search