Advertisement
public char charAt(int index)
// Program to find a character at a particular index of a String.
class StringCharAt
{
public static void main(String[] ar)
{
String str= new String("Keep Smiling");
System.out.println("Original String is "+ str);
//Searching for a character at index 0 in String
char c = str.charAt(0);
System.out.println("character at the index 2 of "+ str + " is " + c);
//Searching for a character at index 5 in String
c = str.charAt(4);
System.out.println("character at the index 4 of "+ str + " is " + c);
//Searching for a character at index 5 in String
c = str.charAt(7);
System.out.println("character at the index 7 of "+ str + " is " + c);
}
}
Original String is Keep Smiling
character at the index 2 of Keep Smiling is K
character at the index 4 of Keep Smiling is
character at the index 7 of Keep Smiling is i
Advertisement
// Program to find a character at a particular index of a String.
class StringCharAt
{
public static void main(String[] ar)
{
String str= new String("Mexico");
//Searching a character at index 9.
char c = str.charAt(9);
System.out.println("Original String is "+ str);
System.out.println("Character at the index 9 of "+ str + " is " + c);
}
}
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.String.charAt(Unknown Source)
at StringCharAt.main(StringCharAt.java:11)
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement