Replacing an Element at a Certain Index with Another Element: replaceAt() Function

Preferences - Do not show ads

The replaceAt() function helps us replace an element at a certain index in a string with another element. Its syntax is:


String.replaceAt(string, new_element, element_index_of_old_element, delimiter);


If the function call succeeds, the replaceAt() function returns a string that is the result of replacing the element at the position element_index_of_old_element with new_element. string is not modified during the process.

If element_index_of_old_element is out of range, replaceAt() will use the nearest valid index instead. For example, if element_index_of_old_element is -2, replaceAt() will replace the element at index 0.

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 replaceAt() better:


var new_str = String.replaceAt("WML Tutorial, WMLScript Tutorial", "WAP", 0, ",");

After executing the above script, the new_str variable has the string value "WAP, WMLScript Tutorial".


var new_str = String.replaceAt("WML Tutorial, WMLScript Tutorial", "WAP", 1, " ");

After executing the above script, the new_str variable has the string value "WML WAP WMLScript Tutorial".


var new_str = String.replaceAt("WML Tutorial, WMLScript Tutorial", "WAP", 10, " ");

After executing the above script, the new_str variable has the string value "WML Tutorial, WMLScript WAP".


Page 50 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