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.
<?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>Code for CharAtExample.wml
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");
};
This example finds the last character in any string you enter into the prompt.