Source of StringBufferChars.java


  1: // Fig. 29.12: StringBufferChars.java
  2: // StringBuffer methods charAt, setCharAt, getChars and reverse.
  3: 
  4: public class StringBufferChars 
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       StringBuffer buffer = new StringBuffer( "hello there" );
  9: 
 10:       System.out.printf( "buffer = %s\n", buffer.toString() );
 11:        System.out.printf( "Character at 0: %s\nCharacter at 4: %s\n\n", 
 12:          buffer.charAt( 0 ), buffer.charAt( 4 ) );
 13: 
 14:       char charArray[] = new char[ buffer.length() ];
 15:       buffer.getChars( 0, buffer.length(), charArray, 0 );
 16:       System.out.print( "The characters are: " );
 17: 
 18:       for ( char character : charArray )
 19:          System.out.print( character );
 20: 
 21:       buffer.setCharAt( 0, 'H' );
 22:       buffer.setCharAt( 6, 'T' );
 23:       System.out.printf( "\n\nbuf = %s", buffer.toString() );
 24: 
 25:       buffer.reverse();
 26:       System.out.printf( "\n\nbuf = %s\n", buffer.toString() );
 27:    } // end main
 28: } // end class StringBufferChars
 29: 
 30: 
 31: /**************************************************************************
 32:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 33:  * Pearson Education, Inc. All Rights Reserved.                           *
 34:  *                                                                        *
 35:  * DISCLAIMER: The authors and publisher of this book have used their     *
 36:  * best efforts in preparing the book. These efforts include the          *
 37:  * development, research, and testing of the theories and programs        *
 38:  * to determine their effectiveness. The authors and publisher make       *
 39:  * no warranty of any kind, expressed or implied, with regard to these    *
 40:  * programs or to the documentation contained in these books. The authors *
 41:  * and publisher shall not be liable in any event for incidental or       *
 42:  * consequential damages in connection with, or arising out of, the       *
 43:  * furnishing, performance, or use of these programs.                     *
 44:  *************************************************************************/