WMLScript » String » String.replace

Syntax:
String.replace(string, oldSubString, newSubString)
string
The mandatory string parameter can be any string containing one or more of any combination of characters and white spaces.
oldSubString
The mandatory oldSubString parameter can be any string containing one or more of any combination of characters and white spaces.
newSubString
The mandatory newSubString parameter can be any string containing zero or more of any combination of characters and white spaces. This parameter can also be the empty string.

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.

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>
   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>
Explanation:

Code for ReplaceExample.wml

Language(s): WML
Code:
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");
};
Explanation:

Code for ReplaceExample.wmls

Language(s): WML

See Also: