Tuesday, October 26, 2010

charAt()

The charAt(int index) method of String returns the character at the specified index. Index 0 is the first character of the String, and the last character is at index length()-1. For example,
String eg = "An Immutable String";

char ch0  = eg.charAt(0);   // ch0 gets 'A'
char ch1  = eg.charAt(1);   // ch1 gets 'n'
char ch5  = eg.charAt(5);   // ch5 gets 'm'

char ch18 = eg.charAt( eg.length()-1 );   // ch18 gets 'g'
char ch17 = eg.charAt( eg.length()-2 );   // ch17 gets 'n'
Notice that the method returns a value of type char.

No comments:

Post a Comment

Chitika