WMLScript » String » String.compare

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

Compares and ranks two strings.

The String.compare function compares two given strings, ranks them based upon their lexicographic relation, and returns a 1, 0, or -1 to indicate their relative rank. By lexicographic relation, we refer to the positional relationship of the characters in the native character set. For example, a is usually ranked lower than b.

  • 1 indicates that string1 is ranked higher that string2.
  • 0 indicates that the two strings are equal in rank.
  • -1 indicates that string1 is ranked lower that string2.

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

<card id="card2">
<p>
   string1 = $(string1)
   <br />
   string2 = $(string2)
   <br />
   rank = $(rank)
</p>
</card>

</wml>
Explanation:

Code for CompareExample.wml

Language(s): WML
Code:
extern function findcompare()
{
   var str1 = Dialogs.prompt("Enter string1", "");
   var str2 = Dialogs.prompt("Enter string2", "");
   var rnk = String.compare(str1, str2);
   WMLBrowser.setVar("string1", str1);
   WMLBrowser.setVar("string2", str2);
   WMLBrowser.setVar("rank", rnk);
   WMLBrowser.go("CharAtExample.wml#card2");
};
Explanation:

Code for CompareExample.wmls

Language(s): WML

See Also: