17.6.3. Inserting an Element at a Certain Position in a String: insertAt() Function

The insertAt() function helps us insert an element at a certain position in a string. Its syntax is:


String.insertAt(string, string_to_be_inserted, element_index, delimiter);


If the function call succeeds, insertAt() returns a string that is the result of inserting string_to_be_inserted to string at the position element_index. A delimiting character will also be inserted if necessary (See the examples below). The insertAt() function does not modify the value of string.

If element_index is out of range, insertAt() will use the nearest valid index instead. For example, if element_index is -2, insertAt() will insert the new 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 insertAt() better:


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

After executing the above script, the new_str variable has the string value "WAP,WML Tutorial, WMLScript Tutorial". Notice that a comma (the delimiter) is inserted between "WAP" and "WML".


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

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


var new_str = String.insertAt("WML Tutorial, WMLScript Tutorial", "WAP", 2, ",");

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


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

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


Previous Page Page 48 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