public class StringBufferInsert
1: // Fig. 29.14: StringBufferInsert.java
2: // StringBuffer methods insert, delete and deleteCharAt.
3:
4: public class StringBufferInsert
5: {
6: public static void main( String args[] )
7: {
8: Object objectRef = "hello";
9: String string = "goodbye";
10: char charArray[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
11: boolean booleanValue = true;
12: char characterValue = 'K';
13: int integerValue = 7;
14: long longValue = 10000000;
15: float floatValue = 2.5f; // f suffix indicates that 2.5 is a float
16: double doubleValue = 33.333;
17:
18: StringBuffer buffer = new StringBuffer();
19:
20: buffer.insert( 0, objectRef );
21: buffer.insert( 0, " " ); // each of these contains new line
22: buffer.insert( 0, string );
23: buffer.insert( 0, " " );
24: buffer.insert( 0, charArray );
25: buffer.insert( 0, " " );
26: buffer.insert( 0, charArray, 3, 3 );
27: buffer.insert( 0, " " );
28: buffer.insert( 0, booleanValue );
29: buffer.insert( 0, " " );
30: buffer.insert( 0, characterValue );
31: buffer.insert( 0, " " );
32: buffer.insert( 0, integerValue );
33: buffer.insert( 0, " " );
34: buffer.insert( 0, longValue );
35: buffer.insert( 0, " " );
36: buffer.insert( 0, floatValue );
37: buffer.insert( 0, " " );
38: buffer.insert( 0, doubleValue );
39:
40: System.out.printf(
41: "buffer after inserts:\n%s\n\n", buffer.toString() );
42:
43: buffer.deleteCharAt( 10 ); // delete 5 in 2.5
44: buffer.delete( 2, 6 ); // delete .333 in 33.333
45:
46: System.out.printf(
47: "buffer after deletes:\n%s\n", buffer.toString() );
48: } // end main
49: } // end class StringBufferInsert
50:
51: /**************************************************************************
52: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
53: * Pearson Education, Inc. All Rights Reserved. *
54: * *
55: * DISCLAIMER: The authors and publisher of this book have used their *
56: * best efforts in preparing the book. These efforts include the *
57: * development, research, and testing of the theories and programs *
58: * to determine their effectiveness. The authors and publisher make *
59: * no warranty of any kind, expressed or implied, with regard to these *
60: * programs or to the documentation contained in these books. The authors *
61: * and publisher shall not be liable in any event for incidental or *
62: * consequential damages in connection with, or arising out of, the *
63: * furnishing, performance, or use of these programs. *
64: *************************************************************************/