toString

Returns a string representing the specified object.

Applies to

Object

Syntax

toString()
toString(radix)

Parameters

radix

(Optional) An integer between 2 and 16 specifying the base to use for representing numeric values.

Description

Every object has a toString method that is automatically called when it is to be represented as a text value or when an object is referred to in a string concatenation. For example, the following examples require theDog to be represented as a string:

Console.Write(theDog)
Console.Write("The dog is " + theDog)

You can use toString within your own code to convert an object into a string, and you can create your own function to be called in place of the default toString method.

Examples

The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary.

for (x = 0; x < 10; x++) {
      ("Decimal: ", x.toString(10), " Binary: ",
Console.write
            x.toString(2))
}

The preceding example produces the following output:

Decimal: 0 Binary: 0
Decimal: 1 Binary: 1
Decimal: 2 Binary: 10
Decimal: 3 Binary: 11
Decimal: 4 Binary: 100
Decimal: 5 Binary: 101
Decimal: 6 Binary: 110
Decimal: 7 Binary: 111
Decimal: 8 Binary: 1000
Decimal: 9 Binary: 1001

See also

Object.valueOf