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"?>
<!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>
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");
};
Code for ReplaceAtExample.wmls