Name

elementAt — Return a single item from a list that is stored as a string

Usage

String.elementAt(str, index, sep)

str : String

index : Number

sep : String

Description

This function processes a string as a list of string values, separated by a specified character (for example: "first string|second|last, final one".)

The first character of sep is used as the separator; str is the string that is processed as a list of strings. The function returns the list item (“element”) specified by index. (The first element has index zero.)

If index is past the end of the list of elements, the last element is returned. Similarly, if index is less than zero, the first element is returned. If index is a floating-point value, it’s first converted to an integer with Float.int( ). Note that an empty str is a valid list containing a single element: the empty string.

Returns invalid if any of the arguments can’t be converted to the appropriate types or if sep is the empty string.

Examples

String.elementAt("a;b;c;d", 2, ";")

returns string "c"

String.elementAt("a;b;c;d", 23, ";")

returns string "d"

String.elementAt("a;b;c;d", -1, ";")

returns string "a"

String.elementAt("a;b;c;d", 1.3, ";")

returns string "b"

String.elementAt("", 1, "x")

returns string ""

String.elementAt(invalid, 1, "x")

returns invalid

String.elementAt("foo", "one", "x")

returns invalid

String.elementAt("foo", 1, invalid)

returns invalid

String.elementAt("foo", 1, "")

returns invalid

Get Learning WML, and WMLScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.