JavaScript by Example: JavaScript Core Objects. Pt. 2 | 4

JavaScript by Example: JavaScript Core Objects. Pt. 2

Search and Replace Methods

In word processing software you'll always find some mechanism to search for patterns in strings and to replace one string with another. JavaScript provides methods to do the same thing, using the String object. The search() method searches for a substring and returns the position where the substring is found first. The match() method searches a string for substrings and returns an array containing all the matches it found. The replace() method searches for a substring and replaces it with a new string. These methods are discussed again in Chapter 13, "Regular Expressions and Pattern Matching," in more detail.

 

<html><head><title>Search and Replace</title>

</head>

<body bgcolor=lightgreen>

<font face="arial" size="+1">

Search and Replace Methods<br>

<font size="-1">

<script language="JavaScript">

1 var straddr = "DanielSavage@dadserver.org";

document.write( "The original string is "+ straddr + "<br>");

document.write( "The new string is "+

2 straddr.replace("Daniel","Jake")+"<br>");

3 var index=straddr.search("dad");

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>

</body></html>

 

 
An e-mail address is assigned to the string variable straddr.

  1. The replace() method takes two arguments, the search string and the replacement string. If the substring Daniel is found, it is replaced with Jake.

  2. The search() method takes a subtring as its argument and returns the first position 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.

  3. The substr() method returns the substring found at position 13, 3 in the string, DanielSavage@dadserver.org: dad.

  4. The substring dad is replaced with POP in the string. (See Figure 9.28.)

The Number Object

Now that we've travelled this far in JavaScript, have you wondered how to format a floating-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, ), which means you can use a primitive number type or an object number type and JavaScript manages the conversion back and forth as necessary. The Number object was introduced in JavaScript 1.1.


The Number() constructor takes a numeric value as its argument. If used as a function, without the new operator, the argument is converted to a primitive numeric value, and that number is returned; if it fails, NaN is returned. The Number object has a number of properties and methods, as listed in Tables 9.11 and 9.12.

 

var number = new Number(numeric value);

var number = Number(numeric value);

 

Example:

 

var n = new Number(65.7);

 

The Number object's properties.

Property

What It Describes

MAX_VALUE

The largest representable number, 1.7976931348623157e+308

MIN_VALUE

The smallest representable number, 5e-324

NaN

Not-a-number value

NEGATIVE_INFINITY

Negative infinite value; returned on overflow

POSITIVE_INFINITY

Infinite value; returned on overflow

prototype

Used to customize the Number object by adding new properties and methods

 

The Number object's methods.

Method

What It Does

toString()

Converts a number to a string using a specified base (radix)

toLocaleString()

Converts a number to a string using local number conventions

toFixed()5

Converts a number to a string with a specified number of places after the decimal point

toExponential()

Converts a number to a string using exponential notation and a specified number of places after the decimal point

toPrecision()

Converts a number to a string in either exponential or fixed notation containing the specified number of places after the decimal point

Using Number Constants and Different Bases

The constants MAX_VALUE, MIN_VALUE, NEGATIVE_INFINITY, POSITIVE_INFINITY, and NaN, are properties of the Number() function, but are not used with instances of the Number object; thus, var huge = Number.MAX_VALUE is valid, but huge.MAX_VALUE is not. NaN is a special value that is returned when some mathematical operation results in a value that is not a number.


The methods provided to the Number object manipulate instances of number objects. For example, to convert numbers to strings representing different bases, the toString() method manipulates a number, either primitive or object. See See .

 

 

 

<html>

<head><title>Number Contants</title>

</head>

<body bgcolor=orange><font color="black" size="+1">

<h2>

Constants

</h2>

<script language="JavaScript">

1 var largest = Number.MAX_VALUE;

2 var smallest = Number.MIN_VALUE;

3 var num1 = 20; // A primitive numeric value

4 var num2 = new Number(13); // Creating a Number object

document.write("<b>The largest number is " + largest+ "<br>");

document.write("The smallest number is "+ smallest + "<br>");

 

 

5 document.write("The number as a string (base 2): "+

num1.toString(2));

6 document.write("<br>The number as a string (base 8): "+

num2.toString(8));

7 document.write("<br>The square root of -25 is: "+

Math.sqrt(-25) + "<br>");

</script>

</body>

</html>

 

 
The constant MAX_VALUE is a property of the Number() function. This constant cannot be used with an instance of a Number object.

  1. The constant MIN_VALUE is a property of the Number() function.

  2. A number is assigned to the variable called num1.

  3. A new Number object is created with the Number() constructor and assigned to num2.

  4. The number is converted to a string represented in binary, base 2.

  5. The number is converted to a string represented in octal, base 8.

  6. The square root of a negative number is illegal. JavaScript returns NaN, not a number, when this calculation is attempted. (See Figure 9.29.)

Constants, number conversion, and NaN.

Created: March 27, 2003
Revised: November 19, 2003

URL: http://webreference.com/programming/java_core/2