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


A button for going back to the top of this page