17.5. Replacing a Substring with Another Substring: replace() Function
The replace() function finds all occurrences of a certain substring and replaces them with another one. The function's syntax is:
String.replace(original_string, old_substring, new_substring);
The replace() function replaces all old_substring in original_string with new_substring. The result is returned if everything works fine. If old_substring is an empty string or an error occurs, replace() returns invalid.
Note that the operation is case-sensitive and original_string will not be modified by the function.
Below are some WMLScript examples that demonstrate how to use the replace() function:
var
str1 = "WMLScript Tutorial";
var str2 =
String.replace(str1, "WMLScript", "WML");
After the execution of the above script, str1 contains the string "WMLScript Tutorial" and str2 contains the string "WML Tutorial".
var str = String.replace("WMLScript Tutorial", "wmlscript", "WML");
After the execution of the above script, str contains the string "WMLScript Tutorial".
var str = String.replace("WMLScript Tutorial", "", "WML");
After the execution of the above script, str contains the invalid value.
Previous Page | Page 44 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