String Character Extraction charAt() method in Java?
Java String Handling
The String Character Extraction: charAt() method
To extract a single character from a String by using charAt() method.
The syntax is as follows:
char charAt(int position)
Where the position is the index of character that you want to obtain.
/* Syntax of charAt() method or Internal implementation of charAt() method*/
public char charAt(int index)
{
if ((index < 0) || (index >= value.length))
{
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
This method returns the character at the specified position.
for example:
char h;
h =“Heera”.charAt(1);
Assigns the value ‘e’ to h.
The charAt() method returns the character at the specified index in a java string.
/* Example: */
public class CharAtExampleByHeera
{
public static void main(String args[])
{
String name="Heera";
char ch=name.charAt(4);
//it returns the char value at the 4th index
System.out.println(ch);
// char at 0 index is H, So output will be "r"
}
}