WMLScript » String » String.replaceAt

Syntax:
String.replaceAt(string, element, index, separator)
string
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.
element
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.
index
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.
separator
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.

Examples

Code:
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.WAPforum.org/DTD/wml_1.1.xml">
<wml>

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

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

</wml>
Language(s): WML
Code:
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);
   WMLBrowser.go("ReplaceAtExample.wml#card2");
};
Explanation:

Code for ReplaceAtExample.wmls

Language(s): WML

See Also: