17.6.2. Getting the Element at a Certain Index in a String: elementAt() Function
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 "".
Previous Page | Page 47 of 71 | Next Page | ![]() |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data