WMLScript » String » String.replaceAt

String.replaceAt(string, element, index, separator)
The mandatory string parameter can be any string containing zero or more of any combination of characters and white spaces. This parameter can be an empty string.
The mandatory element parameter is a substring that you wish to add to the string of the string parameter. This parameter can be an empty string.
The mandatory index parameter is the element position in the string array. If the provided index number is less than zero, then the given element will be appended onto the beginning of the given string. If the provided index number is greater than the actual number of elements, then the given element will be appended onto the end of the given string.
The mandatory separator parameter can be zero or more of any character or white space that you wish to use to divide the string into substrings called elements. This parameter can be an empty string.

Replaces a specified occurrence of a substring with a new substring.

The String.replaceAt function removes an element from a string at the specified index position, inserts a new element, and returns the modified string.

This function treats a string as an array composed of elements delimited by a separator that is composed of one or more specified characters (including white space). The first index position is numbered zero and the last index position is the total number of the elements minus one. The total number of elements in the string can be found by using the String.elements function.

You can use the String.replace function to replace each occurrence of a specified element in a string with a new element (new substring). The String.insertAt function can be used to add a element to a given string.


<?xml version="1.0"?>

<card id="card1">
   replaceAt example
   <do type="accept">
      <go href="ReplaceAtExample.wmls#findreplaceat()" />

<card id="card2">
   old string = $(oldstring)
   <br />
   element = $(element)
   <br />
   index = $(indx)
   <br />
   separator = $(sepa)
   <br />
   new string = $(newstring)

Language(s): WML
extern function findreplaceat()
   var str = Dialogs.prompt("Enter a string", "Hello wired world");
   var ele = Dialogs.prompt("Enter new element", "wireless");
   var ind = Dialogs.prompt("Enter index", "1");
   var sep = Dialogs.prompt("Enter separator", " ");
   var newstr = String.replaceAt(str, ele, ind, sep);
   WMLBrowser.setVar("oldstring", str);
   WMLBrowser.setVar("element", ele);
   WMLBrowser.setVar("indx", ind);
   WMLBrowser.setVar("sepa", sep);
   WMLBrowser.setVar("newstring", newstr);

Code for ReplaceAtExample.wmls

Language(s): WML

See Also: