String Methods
Continuing with commonly used method in the stings we are covering here charAt() method is more detailed manner.
Continuing with commonly used method in the stings we are covering here charAt() method is more detailed manner.
- String charAt()
- The charAt() method returns the character at the specified index number. The default index in which a string start is zero.
public class Main{
public static void main(String args[]){
String s="Learn Selenium";
char character=s.charAt(4);// Returns the char value at the 4th index
System.out.println(character);
}
}
The output of the above program is n
Now what if you try to put the index value which is out of range and does not return any string value
public class Main{
public static void main(String args[]){
String s="Learn Selenium";
char character=s.charAt(15);// Returns Exception
System.out.println(character);
}
}
The output of the above program is below exception [PS: you have to be very careful while supplying the index no ]
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 15
at java.lang.String.charAt(String.java:658)
at Main.main(Main.java:4)
0 comments:
Post a Comment