WMLScript » String » String.insertAt

Syntax:
String.insertAt(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 any character or white space that you wish to use to divide the string into substrings called elements. This parameter can be the empty string.

Inserts an element and separator into a string.

The String.insertAt function inserts the given element and separator into the given string at the given index position 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.

When the new element is inserted, it will be delimited on each end by the separator. However, if the element is added to the start of the string, there will be no leading separator. If the element is added to the end of the string, there will be no trailing separator.

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>
   insertAt example
   </p>
   <do type="accept">
      <go href="InsertAtExample.wmls#findinsertat()" />
   </do>
</card>

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

</wml>
Explanation:

Code for InsertAtExample.wml

Language(s): WML
Code:
extern function findinsertat()
{
   var oldstr = Dialogs.prompt("Enter a string", "Hello world!");
   var elem = Dialogs.prompt("Enter an element", "wireless");
   var indx = Dialogs.prompt("Enter an index", "1");
   var sep = Dialogs.prompt("Enter a separator", " ");
   var newstr = String.insertAt(oldstr, elem, indx, sep);
   WMLBrowser.setVar("oldstring", oldstr);
   WMLBrowser.setVar("element", elem);
   WMLBrowser.setVar("index", indx);
   WMLBrowser.setVar("separator", sep);
   WMLBrowser.setVar("newstring", newstr);
   WMLBrowser.go("InsertAtExample.wml#card2");
};
Explanation:

Code for InsertAtExample.wmls

Language(s): WML

See Also: