WMLScript » String » String.charAt

Syntax:
String.charAt(string, index)
string
The mandatory string parameter can be any string containing one or more of any combination of characters and white spaces. (This parameter can also be the empty string. However, the empty string has no index and you cannot return any character.)
index
The mandatory index parameter is the position in the string array.

Returns the character at the specified location in the string.

The String.charAt function finds and returns the character at the given index position in the given string.

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 you attempt to find a character at an index position that does not exist, no error will occur and the empty string "" 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>
   charAt example
   </p>
   <do type="accept">
      <go href="CharAtExample.wmls#findcharat()" />
   </do>
</card>

<card id="card2">
<p>
   index $(lastposition) is $(lastchar)
</p>
</card>

</wml>
Explanation:

Code for CharAtExample.wml

Language(s): WML
Code:
extern function findcharat()
{
   var str = Dialogs.prompt("Enter a string", "");
   var last = String.length(str) - 1;
   WMLBrowser.setVar("lastposition", last);
   var char = String.charAt(str, last);
   WMLBrowser.setVar("lastchar", char);
   WMLBrowser.go("CharAtExample.wml#card2");
};
Explanation:

This example finds the last character in any string you enter into the prompt.

Language(s): WML

See Also: