WMLScript » String » String.subString

String.subString(string, startIndex, length)
The mandatory string parameter can be any string containing one or more of any combination of characters and white spaces.
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.
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.


<?xml version="1.0"?>

<card id="card1">
   subString example
   <do type="accept">
      <go href="SubStringExample.wmls#findsubstring()" />

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


Code for SubStringExample.wml

Language(s): WML
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);

Code for SubStringExample.wmls

Language(s): WML

See Also: