public
synchronized char charAt(int index)
Returns
the char value in this sequence at the specified index.
public class StringBufferCharAt { public static void main(String args[]){ StringBuffer s1 = new StringBuffer("Hello"); for(int i=0; i<s1.length(); i++){ System.out.print("Character at index " + i + " is "); System.out.println(s1.charAt(i)); } } }
Output
Character at index 0 is H Character at index 1 is e Character at index 2 is l Character at index 3 is l Character at index 4 is o
1. Throws
IndexOutOfBoundsException, if index is negative or greater than or
equal to length().
public class StringBufferCharAtIndexOut { public static void main(String args[]){ StringBuffer s1 = new StringBuffer("Hello"); System.out.println("Character at index 5 is "); System.out.println(s1.charAt(5)); } }
Output
Character at index 5 is Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.StringBuffer.charAt(StringBuffer.java:204) at StringBufferCharAtIndexOut.main(StringBufferCharAtIndexOut.java:16) Java Result: 1
No comments:
Post a Comment