... Method setCharAt (lines 20 and 21) takes an integer and a character argument and sets the character at the specified position in the StringBuilder to the character argument. Method reverse (line 24) reverses the contents of the StringBuilder. Attempting to access a character that’s outside the bounds of a StringBuilder results in a StringIndexOutOfBoundsException.

Fig. 14.12 StringBuilder methods charAt, setCharAt, getChars and reverse


 1   // Fig. 14.12: StringBuilderChars.java
 2   // StringBuilder methods charAt, setCharAt, getChars and reverse.
 3
 4   public class StringBuilderChars {
 5      public static void main(String[] args) {
 6         StringBuilder buffer = new StringBuilder("hello there");
 7
 8         System.out.printf("buffer = %s%n", buffer.toString());
 9 System.out.printf( ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.