Getting the Element at a Certain Index in a String: elementAt() Function

Preferences - Do not show ads

The elementAt() function is used to get the element at a certain index in a string. Its syntax is:


String.elementAt(string, element_index, delimiter);


The string and delimiter parameters are used in the same way as before. If the function call succeeds, the element at the position element_index is returned. (The element index counts from 0.) If element_index is out of range, then the element at the nearest valid index is returned. For example, if element_index is -2, the element at index 0 will be returned.

If delimiter is an empty string or an error occurs, an invalid value is returned.

Below are some WMLScript examples that can help you understand the usage of elementAt() better:


var element = String.elementAt("- WML Tutorial, WMLScript Tutorial -", 0, ",");

After executing the above script, the element variable has the string value "- WML Tutorial".


var element = String.elementAt("- WML Tutorial, WMLScript Tutorial -", 100, ",");

After executing the above script, the element variable has the string value " WMLScript Tutorial -".


var element = String.elementAt("- WML Tutorial, WMLScript Tutorial -", 1, " ");

After executing the above script, the element variable has the string value "WML".


var element = String.elementAt("- WML Tutorial, WMLScript Tutorial -", 0, "-");

After executing the above script, the element variable has the string value "".


Page 47 of 71 Previous Page | Next Page
Contents at a Glance
Print this Web Page | | Back to Top

Feedback Form (Expand)

What do you think about this web page?






WMLScript Tutorial Table of Contents WMLScript Tutorial Contents at a Glance Preferences - Change Color Scheme Preferences - Do Not Show Ads Previous Page Next Page