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


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page