3. WMLScript String Standard Library Quick Reference

WMLScript's String standard library provides various functions that help us manipulate strings.



WMLScript Quick Reference: Functions in the String Standard Library

charAt(string, character_index)

Returns the character at the position character_index of string. The character index of a string counts from 0.

Example:

var character = String.charAt("WMLScript Reference", 1);

The character variable has the string value "M".

compare(string1, string2)

Finds out whether string1 is equal to (return value is 0), smaller than (return value is -1) or larger than (return value is 1) string2.

Examples:

var x = String.compare("a", "a");

x has the integral value 0.

var y = String.compare("a", "b");

y has the integral value -1.

var z = String.compare("c", "b");

z has the integral value 1.

elementAt(string, element_index, delimiter)

Returns the element at the position element_index in string using the first character of delimiter as the separator of the elements. The element index counts from 0.

Example:

var element1 = String.elementAt("WML WMLScript Reference", 0, " ");
var element2 = String.elementAt("WML WMLScript Reference", 2, " ");

element1 has the string value "WML".
element2 has the string value " Reference".

elements(string, delimiter)

Finds the number of elements in string using the first character of delimiter as the separator of the elements.

var elementNum1 = String.elements("WML WMLScript Reference", " ");
var elementNum2 = String.elements("", ",");

elementNum1 has the integral value 3.
Note that elementNum2 has the integral value 1 since an empty string is considered as a valid element.

find(string, substring)

Checks whether substring can be found in string. If yes, it returns the character index of the first character of the match, otherwise it returns -1. The character index of a string counts from 0.

Example:

var x = String.find("WMLScript Reference", "Script");
var y = String.find("WMLScript Reference", "script");

x has the integral value 3.
y has the integral value -1.

format(string, value)

Returns a string that is the result of inserting value to string. A format specifier is used to define the formatting. The form of a format specifier is: (arguments in brackets are optional)

%[width][.precision]type

Three characters can be specified as type. They are d, f, and s. Use d if value is an integer, f if value is a floating-point number and s if value is a string.

Examples:

var u = String.format("The result of 1+2 is %d", 1+2);

u has the string value "The result of 1+2 is 3".

var v = String.format("The result of 1+2 is %s", "3");

v has the string value "The result of 1+2 is 3".

var w = String.format("The result of 1+2 is %5d", 1+2);

w has the string value "The result of 1+2 is     3".

var x = String.format("The result of 1+2 is %10d", 1+2);

x has the string value "The result of 1+2 is          3".

var y = String.format("%.2f", 12.3456789);

y has the string value "12.35".

var z = String.format("%.3f", 12.3456789);

z has the string value "12.346".

insertAt(string, string_to_be_inserted, element_index, delimiter)

Returns a string that is the result of inserting string_to_be_inserted to string at the position element_index. The delimiting character will also be inserted if necessary. The element index counts from 0. The first character of delimiter is used to separate elements in string.

Example:

var x = String.insertAt("WMLScript Reference", "WML", 0, " ");

x has the string value "WML WMLScript Reference".

isEmpty(string)

Checks whether string is an empty string.

Example:

var isEmptyString = String.isEmpty("");

isEmptyString has the Boolean value true.

length(string)

Returns the number of characters contained in string.

Example:

var charactersNum = String.length("WMLScript Reference");

charactersNum has the integral value 19.

removeAt(string, element_index, delimiter)

Returns a string that is the result of removing the element at the position element_index from string. The element index counts from 0. The first character of delimiter is used to separate elements in string.

Example:

var x = String.removeAt("WML WMLScript Reference", 0, " ");

x has the string value "WMLScript Reference".

replace(original_string, old_substring, new_substring)

Replaces all old_substring in original_string with new_substring.

Example:

var x = String.replace("WML Tutorial, WMLScript Tutorial", "Tutorial", "Reference");

x has the string value "WML Reference, WMLScript Reference".

replaceAt(string, new_element, element_index_of_old_element, delimiter)

Returns a string that is the result of replacing the element at the position element_index_of_old_element with new_element. The element index counts from 0. The first character of delimiter is used to separate elements in string.

Example:

var x = String.replaceAt("WML WMLScript Reference", "WAP", 0, " ");

x has the string value "WAP WMLScript Reference".

squeeze(string)

Reduces every whitespace in string to a single space.

Example:

var x = String.squeeze("WMLScript      \n   Reference");

x has the string value "WMLScript Reference".

subString(string, character_index, length)

Returns a substring from string. The substring starts at character_index of string. length is the number of characters of the substring. The character index of a string counts from 0.

Example:

var substring = String.subString("WMLScript Reference", 3, 6);

substring has the string value "Script".

toString(value)

Converts value to the string data type.

Example:

var string1 = String.toString(10+5);
var string2 = String.toString(10 > 1);

string1 has the string value "15".
string2 has the string value "true".

trim(string)

Removes all leading and trailing whitespaces in string.

Example:

var string1 = String.trim("   \n  WMLScript Reference       ");

string1 has the string value "WMLScript Reference".



Previous Page Page 3 of 6 Next Page


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page