17.4. Finding if a Certain Substring Exists in a String: find() function

The find() function helps us find if a certain substring exists in a string. If yes, it returns the index of the first character of the match in the string, otherwise it returns -1. The function's syntax is:


String.find(string, substring);


The find() function returns invalid if substring is an empty string or if any error occurs.

Note that the operation is case-sensitive.

The following WMLScript examples demonstrate how to use the find() function:


var index = String.find("WMLScript Tutorial", "WML");

After the execution of the above script, index contains the integral value 0.


var index = String.find("WMLScript Tutorial", "wml");

After the execution of the above script, index contains the integral value -1.


var index = String.find("WMLScript Tutorial", "");

After the execution of the above script, index contains invalid.


Previous Page Page 43 of 71 Next Page


A button for going back to the top of this page