Description
public char charAt(int index): This method returns the character present at the specified index in the StringBuilder instance. The first char of StringBuilder
is at index 0, second one is at index 1 and so on. The index argument must be greater than or equal to 0, and less than the length of the sequence, otherwise it would throw IndexOutOfBoundsException
Example
This example shows, how to find the character of a particular index in a StringBuilder
instance.
import java.lang.StringBuilder; class StringBuilderCharAt{ public static void main(String[] args) { StringBuilder sb = new StringBuilder("TEST STRING"); System.out.println("String is: "+sb); for(int i=0; i<sb.length(); i++){ System.out.println("Character at index "+i+ " is: "+sb.charAt(i)); } } }
Output:
String is: TEST STRING Character at index 0 is: T Character at index 1 is: E Character at index 2 is: S Character at index 3 is: T Character at index 4 is: Character at index 5 is: S Character at index 6 is: T Character at index 7 is: R Character at index 8 is: I Character at index 9 is: N Character at index 10 is: G
Leave a Reply