Chapter 3 DynaScript Predefined Objects


String methods

The String object has these methods:

charAt method

Syntax

String.charAt( position )

Description

Returns the character in the string located at position. The position is 0 indexed.

Return

Character.

Example

This example displays the character e:

<!--SCRIPT 
myString = new String("hello");
document.WriteLn(myString.charAt(1));
-->

indexOf method

Syntax

String.indexOf( substring [, position] )

Description

Searches for substring within a string. You can optionally specify the position that the search is to begin. If not specified, the search begins at 0.

Return

An integer indicating the start location of the substring. If the substring is not found, -1 is returned.

Example

This example has a return of 3:

<!--SCRIPT 
myString = new String("hello world");
x = myString.indexOf("lo");
document.WriteLn(x);
-->

lastIndexOf method

Syntax

String.lastIndexOf( substring [, position] )

Description

Searches for the last occurrence of a substring within a script. The position that the search is to begin may be optionally specified. If not specified, the search will begin at 0.

Return

An integer indicating the start location of the last substring found. If the substring is not found, -1 is returned.

Example

This example has a return of 14:

<!--SCRIPT 
myString = new String( "hello world hello" );
x = myString.lastIndexOf( "ll" );
document.WriteLn( x );
-->

split method

Syntax

String.split( separatorString )

Description

Separates a string at the separatorString and returns the resulting substrings as an Array object. The separatorString characters are not part of the returned substrings.

If an empty separator string is provided (" ") as the separator string, the string is split into an array of chars.

Return

An Array object.

Example

This example splits a string each time the separator string 'split' is encountered:

<!--SCRIPT 
myString = new String( "This string will split each time the string 'split' is encountered" );
splitString = myString.split( "split" );
document.WriteLn( splitString );
-->

The output from this example looks similar to:

{ This string will ,  each time the string ', ' is encountered }

substring method

Syntax

String.substring( start )

Description

Returns a substring value that is extracted from the string object starting at the start value (numeric position) and continuing to the end of the string object.

Return

String.

Example

This example returns "hello":

<!--SCRIPT 
myString = new String( "Hello world, hello" );
sampleString = myString.substring( 13 );
document.WriteLn( sampleString );
-->

substring method

Syntax

String.substring( start, end )

Description

Returns a substring value that is extracted from the string object starting at the start value and ending (but not including) the end value.

Return

String.

Example

This example returns "world":

<!--SCRIPT 
myString = new String( "Hello world, hello" );
sampleString = myString.substring( 5, 11 );
document.WriteLn( sampleString );
-->

toLowerCase method

Syntax

String.toLowerCase( )

Description

Converts a string object to an entirely lower-case string.

Return

String.

Example

This example returns of "hello, world hello":

<!--SCRIPT 
myString = new String( "Hello world, hello" );
sampleString = myString.toLowerCase();
document.WriteLn( sampleString );-->
-->

toString method

Syntax

String.toString( )

Description

Returns the string value of the object.

Return

String.

Example

This example returns "Hello world, hello":

<!--SCRIPT 
myString = new String( "Hello world, hello" );
document.WriteLn( myString.toString() );
-->

toUpperCase method

Syntax

String.toUpperCase( )

Description

Converts a string object to upper case.

Return

String.

Example

This example will returns of "HELLO WORLD, HELLO":

<!--SCRIPT 
myString = new String( "Hello world, hello" );
sampleString = myString.toUpperCase();
document.WriteLn( sampleString );
-->

valueOf method

Syntax

String.valueOf( )

Description

Returns the string value.

Return

String.

Example

 


Copyright © 1999 Sybase, Inc. All rights reserved.