String.charAt() (Method)

A character within the string

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise server - 2.0
Opera - 3.0
Property/method value type:String primitive
JavaScript syntax:-myString.charAt(aPosition)
Argument list:aPositionA valid character position within the string

The character at the position in the string indicated by the argument value is returned by this method.

This may involve converting the receiving object to a string first. This means it can be used generically on many kinds of object.

If there is no character at that position, an empty string will be returned.

The result will be a string value and not a String object.

Note that there is no complementary setCharAt() method because strings are immutable and cannot be changed.

Example code:

   <HTML>

   <HEAD>

   </HEAD>

   <BODY>

   <TABLE BORDER=1>

   <TR>

   <TH>Index</TH>

   <TH>Char</TH>

   </TR>

   <SCRIPT>

   myString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   for(myEnum=0; myEnum<myString.length; myEnum++)

   {

      document.write("<TR>");

      document.write("<TD>");

      document.write(myEnum);

      document.write("</TD>");

      document.write("<TD>");

      document.write(myString.charAt(myEnum));

      document.write("</TD>");

      document.write("</TR>");

   }

   </SCRIPT>

   </TABLE>

   </BODY>

   </HTML>

See also:Character handling, Character testing, File.stringToByte(), String.prototype, Window.atob()

insert figure 0025

Cross-references:

ECMA 262 edition 2 - section - 15.5.4.4

ECMA 262 edition 3 - section - 15.5.4.4