WMLScript » String » String.subString

Syntax:
String.subString(string, startIndex, length)
string
The mandatory string parameter can be any string containing one or more of any combination of characters and white spaces.
startIndex
The mandatory startIndex parameter is an integer number that is the position in the string array where the substring begins. If you provide a negative number, the index will start at zero. If the index number is greater in value than the highest actual index number in the string, an empty string will be returned and no error will be generated.
length
The mandatory length parameter is an integer number that is the overall length (size) of the substring. If the combination of the start index and length calls for a substring that would extend past the actual end of the given string, then a truncated substring is returned and no error is generated.

Creates a new string by selecting a substring out of an existing string.

The String.subString function returns a substring that is contained in a given string. You must specify both the starting position inside the string and the length of the substring.

This function treats a string as an array where the first index position is numbered zero and the last index position is the total length of the string minus one. The total length of the string can be found using the String.length function.

You can use the String.find function to return the index position of a specified substring in a 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>
   subString example
   </p>
   <do type="accept">
      <go href="SubStringExample.wmls#findsubstring()" />
   </do>
</card>

<card id="card2">
<p>
   string = $(strng)
   <br />
   start index = $(startindx)
   <br />
   length = $(lngth)
   <br />
   substring = $(substrng)
</p>
</card>

</wml>
Explanation:

Code for SubStringExample.wml

Language(s): WML
Code:
extern function findsubstring()
{
   var str = Dialogs.prompt("Enter a string", "");
   var ind = Dialogs.prompt("Enter start index", "");
   var len = Dialogs.prompt("Enter length", "");
   var substr = String.subString(str, ind, len);
   WMLBrowser.setVar("strng", str);
   WMLBrowser.setVar("startindx", ind);
   WMLBrowser.setVar("lngth", len);
   WMLBrowser.setVar("substrng", substr);
   WMLBrowser.go("SubStringExample.wml#card2");
};
Explanation:

Code for SubStringExample.wmls

Language(s): WML

See Also: