Replaces all occurrences of a substring with a new substring.
The
String.replace function searches the given string, removes each
occurrence of the designated old substring, replaces each occurrence
with a designated new substring, and returns the modified string.
You can use the String.replaceAt
function to replace a specified element in a string with a new
substring (new element). The String.subString function can be
used to return a substring out of 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>
replace
example
</p>
<do type="accept">
<go
href="ReplaceExample.wmls#findreplace()" />
</do>
</card>
<card id="card2">
<p>
string = $(oldstring)
<br />
old
substring = $(oldsubstr)
<br
/>
new substring = $(newsubstr)
<br />
new
string = $(newstring)
</p>
</card>
</wml>Code for ReplaceExample.wml
extern function
findreplace()
{
var oldstr =
Dialogs.prompt("Enter a string", "Hello wired world!");
var oldsub = Dialogs.prompt("Enter old substring",
"wired");
var newsub =
Dialogs.prompt("Enter new substring", "wireless");
var newstr = String.replace(oldstr, oldsub, newsub);
WMLBrowser.setVar("oldstring", oldstr);
WMLBrowser.setVar("oldsubstr", oldsub);
WMLBrowser.setVar("newsubstr", newsub);
WMLBrowser.setVar("newstring", newstr);
WMLBrowser.go("ReplaceExample.wml#card2");
};
Code for ReplaceExample.wmls