WMLScript » String » String.find

Syntax:
String.find(string, substring)
string
The mandatory string parameter can be any string containing zero or more of any combination of characters and white spaces.
substring
The mandatory substring parameter can be any string containing zero or more of any combination of characters and white spaces.

Returns the location of a specified substring in a string.

The String.find function searches a given string for the given substring and returns the index position of the first occurrence of the substring. If no match is made, then a -1 is returned.

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.

If either the string or substring parameters is the empty string "", or if both parameters are the empty string "", a -1 is returned.

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

<card id="card2">
<p>
   string = $(strng)
   <br />
   substring = $(substring)
   <br />
   index = $(indexnum)
</p>
</card>

</wml>
Explanation:

Code for FindExample.wml

Language(s): WML
Code:
extern function findfind()
{
   var str = Dialogs.prompt("Enter a string", "");
   var sub = Dialogs.prompt("Enter a substring", "");
   var num = String.find(str, sub);
   WMLBrowser.setVar("strng", str);
   WMLBrowser.setVar("substring", sub);
   WMLBrowser.setVar("indexnum", num);
   WMLBrowser.go("FindExample.wml#card2");
};
Explanation:

Code for FindExample.wmls

Language(s): WML

See Also: