charAt Java: This tiny method in Java looks tiny, but does a great job. Yes! just compare it with the logic that we have done in C Programming, how is it do this? Oh My God!, but now Java, our friend knowing the difficulty, created the method charAt(int), to determine what character is at the given index. Looking simple? The program will too :)
st.charAt(5): This method belongs to the java.lang.String class, it is called on st, so st will be considered as the string to search, next it is to find the char at the position 5 starting from 0 i.e. J is in the 0th position and a is at the 1 position in the above string and so on. This returns the charAt the given position.
----------------------------------
Output
----------------------------------
The char at '5' is c
class JavaCharAt
{
public static void main(String args[])
{
// Create a string
String st="Java charAt(int) Example";
// Get the charAt 5 (for instance)
char c=st.charAt(5);
// Print the char
System.out.println("The char at '5' is "+c);
}
}
st.charAt(5): This method belongs to the java.lang.String class, it is called on st, so st will be considered as the string to search, next it is to find the char at the position 5 starting from 0 i.e. J is in the 0th position and a is at the 1 position in the above string and so on. This returns the charAt the given position.
----------------------------------
Output
----------------------------------
The char at '5' is c