Thursday, 2 June 2011

WMLScript String Library


WMLScript String Library
The String library contains functions for manipulating text.
WMLScript String Library Functions
Function
Description
Returns the character at a specified position
Compares two strings and returns a value that represents the result of the comparison
Divides a string into elements and returns a specified element
Returns the number of times a specified value appears in a string
Returns the position of a substring in a string
Formats a value
Divides a string into elements and inserts a substring at a specified index position
Checks whether a string is empty
Returns the length of a string

 

1)               WMLScript charAt() Function

The charAt() function returns the character at the specified index position.

Syntax

n = String.charAt(string, index)

Part
Description
n
The string returned from the function
string
A string
index
A number that specifies the index position in the string

Example

var a = String.charAt("world",2);
var b = String.charAt("world",0);
var c = String.charAt("world",10);

Result

a = "r"
b = "w"
c = ""

2)               WMLScript compare() Function

The compare() function compares two strings and returns a value that represents the result of the comparison.
The compare() function can return one of the following values:
  • -1 (if string1 < string2)
  • 0 (if string1 = string2)
  • 1 (if string1 > string2)

Syntax

n = String.compare(string1, string2)

Part
Description
n
The integer returned from the function
string1
A string
string2
A string

Example

var a = String.compare("world","world");
var b = String.compare("I","world");
var c = String.compare("world","I");

Result

a = 0
b = -1
c = 1

3)               WMLScript elementAt() Function

The elementAt() function divides a string into elements and returns the element at the specified index position.

Syntax

n = String.elementAt(string, index, separator)

Part
Description
n
The string returned from the function
string
The string to be parsed 
index
An integer defining the part to be returned
separator
The separator that divides the strings

Example

NOTE: If index is negative, the first element is returned. If index is too high, the last element is returned.
var a= String.elementAt("Visit W3Schools today",0," ");
var b= String.elementAt("Visit W3Schools today",1," ");
var c= String.elementAt("Visit W3Schools today",2," ");
var d= String.elementAt("Apples+Bananas",0,"+");
var e= String.elementAt("Apples+Bananas",1,"+");
var f= String.elementAt("Apples+Bananas",5,"+");

Result

a = "Visit"
b = "W3Schools"
c = "today"
d = "Apples"
e = "Bananas"
f = "Bananas"

4)               WMLScript elements() Function

The elements() function returns the number of times a specified value appears in a string.

Syntax

n = String.elements(string, separator)

Part
Description
n
An integer that specifies the number of times the value specified in the separator parameter appears in the string parameter
string
The string to be searched
separator
The string value to search for in the string

Example

var a = String.elements("Visit W3Schools!","i");
var b = String.elements("Visit W3Schools!","V");
var c = String.elements("Visit W3Schools!"," ");

Result

a = 2
b = 1
c = 1

5)               WMLScript find() Function

The find() function returns the position of a substring in a string.

Syntax

n = String.find(string, substring)

Part
Description
n
The integer returned from the function
string
The string to search
substring
The value to search for in the string

Example

var a = String.find("world","rl");
var b = String.find("world","hi");
var c = String.find("world","wo");

Result

a = 2
b = -1
c = 0

6)               WMLScript format() Function

The format() function formats a value.

Syntax

n = String.format(format, value)

Part
Description
n
The string returned from the function
format
Specifies how to format the value.
The format consists of three parts: %width.precision type
width - Optional. Specifies the minimum number of characters printed.
precision - Optional. Sets the precision of the output value. Can take one of the following values:
  • d (The minimum number of digits to print. Default is 1)
  • f (The number of digits after the decimal point. Default is 6)
  • s (The maximum number of characters to print. All characters are printed by default)
type - Required. Determines how to interpret the formatted value. Can take one of the following values:
  • d (Integer - the output value has nd digits)
  • f (Floating-Point - the output value has nf decimal digits)
  • s (String - Characters are printed to the end of the string or until the precision value is reached)
value
The value to be formatted

Example

var b = String.format("%4.3d", 32);
var d = String.format("%3f", 10.1234);
var e = String.format("%2.2f", 2.3)

Result

b = " 032"
d = "10.123"
e = "2.30"

7)               WMLScript insertAt() Function

The insertAt() function divides a string into elements and inserts a substring at the specified index position.

Syntax

n = String.insertAt(string, substring, index, separator)

Part
Description
n
The string returned from the function
string
The original string
substring
The substring to be inserted into the original string
index
An integer that specifies where to insert the substring
separator
The dividing separator

Example

var a = String.insertAt("Visit W3Schools!","us at",1," ");

Result

a = "Visit us at W3Schools!"

8)               WMLScript length() Function

The length() function returns the length of a string.

Syntax

n = String.length(string)

Part
Description
n
The length of the string
string
A string

Example

var a = String.length("");
var b = String.length("Hello world");
var c = String.length(23.4);

Result

a = 0
b = 11
c = 4

No comments:

Post a Comment