WMLScript » String » String.elements

Syntax:
String.elements(string, separator)
string
The mandatory string parameter can be any string containing zero or more of any combination of characters and white spaces. The empty string "" is usually recognized as one element.
separator
The mandatory separator parameter can be one or more characters, including one or more of any type of white space characters, that you wish to use to divide the string into substrings called elements. (Depending on the browser, if the separator is set to be the empty string, either invalid or 1 will be returned.)

Returns the number of occurrences of a specified character element.

The String.elements function determines and then returns the number of elements (substrings) that are separated by the given separator and are contained in the specified 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).

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>
   elements example
   </p>
   <do type="accept">
      <go href="ElementsExample.wmls#findelements()" />
   </do>
</card>

<card id="card2">
<p>
   string = $(strng)
   <br />
   separator = $(separator)
   <br />
   # of elements = $(numelements)
</p>
</card>

</wml>
Explanation:

Code for ElementsExample.wml

Language(s): WML
Code:
extern function findelements()
{
   var str = Dialogs.prompt("Enter a string", "Hello wireless world!");
   var sep = Dialogs.prompt("Enter a separator", " ");
   var numelem = String.elements(str, sep);
   WMLBrowser.setVar("strng", str);
   WMLBrowser.setVar("separator", sep);
   WMLBrowser.setVar("numelements", numelem);
   WMLBrowser.go("ElementsExample.wml#card2");
};
Explanation:

Code for ElementsExample.wmls

Language(s): WML

See Also: