FUNCTION: String.charAt
String.charAt(string,
index)
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.
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.)
The mandatory index parameter is the position in the string array.
This example finds the last character in any string you enter into the
prompt.
Code for CharAtExample.wml
<?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.wmls
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");
};
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information
|