HTML and CSS Reference
In-Depth Information
EXAMPLE 9.30 ( CONTINUED )
document.write("The search() method found \"dad\" at
position "+ index +"<br />");
4
var mysubstr=straddr.substr(index,3) ;
document.write("After replacing \"dad\" with \"POP\" <br />");
5
document.write( straddr.replace(mysubstr,"POP") +"<br />");
</script>
</small>
</font>
</body>
</html>
EXPLANATION
1
An e-mail address is assigned to the string variable straddr .
2
The replace() method takes two arguments, the search string and the replacement
string. If the substring Daniel is found, it is replaced with Jake .
3
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 DanielSavage@dadserver.org and is found at position 13.
4
The substr() method returns the substring found at position 13, 3 in the string,
DanielSavage@dadserver.org : dad.
5
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