WMLScript » String » String.elementAt

Syntax:
String.elementAt(string, index, separator)
string
The mandatory string parameter can be any string containing zero or more of any combination of characters and white spaces.
index
The mandatory index parameter is the position of the element in the string array. A negative index number will return the first element and not generate an error. An index number larger than the actual number of element will return the last element and not generate an error.
separator
The mandatory separator parameter can be any character, white space, or the empty string that you wish to use to divide the string into substrings called elements.

Returns the specified portion of a string.

The String.elementAt function finds and returns the string element at the given index position in the given string.

This function treats a string as an array composed of elements delimited by a separator that is composed of one or more specified characters (including white space). The first index position is numbered zero and the last index position is the total number of the elements minus one. The total number of elements in the string can be found by using the String.elements function.

If the string is empty, the empty string "" is returned.

Examples

Code:
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.WAPforum.org/DTD/wml_1.1.xml">
<wml>

<card id="card1">
   <p>
   elementAt example
   </p>
   <do type="accept">
      <go href="ElementAtExample.wmls#findelementat()" />
   </do>
</card>

<card id="card2">
<p>
   string = $(strng)
   <br />
   index = $(index)
   <br />
   separator = $(separator)
   <br />
   element = $(element)
</p>
</card>

</wml>
Explanation:

Code for ElementAtExample.wml

Language(s): WML
Code:
extern function findelementat()
{
   var str = Dialogs.prompt("Enter a string", "Hello wireless world!");
   var ind = Dialogs.prompt("Enter an index", "1");
   var sep = Dialogs.prompt("Enter a separator", " ");
   var elem = String.elementAt(str, ind, sep);
   WMLBrowser.setVar("strng", str);
   WMLBrowser.setVar("index", ind);
   WMLBrowser.setVar("separator", sep);
   WMLBrowser.setVar("element", elem);
   WMLBrowser.go("ElementAtExample.wml#card2");
};
Explanation:

Code for ElementAtExample.wmls

Language(s): WML

See Also: