Chapter 10 Strings and Characters 567 any position (Web site)

Chapter 10 Strings and Characters 567 any position in a StringBuffer. Method delete takes two arguments the starting subscript and the subscript one past the end of the characters to delete. All characters beginning at the starting subscript up to, but not including the ending subscript are deleted. Method deleteCharAttakes one argument the subscript of the character to delete. Invalid subscripts cause both methods to throw a StringIndexOutOfBounds- Exception. The insertand delete methods are demonstrated in Fig. 10.16. 1 // Fig. 10.16: StringBufferInsert.java 2 // This program demonstrates the insert and delete 3 // methods of class StringBuffer. 4 5 // Java extension packages 6 import javax.swing.*; 7 8 public class StringBufferInsert { 9 10 // test StringBuffer insert methods 11 public static void main( String args[] ) 12 { 13 Object o = “hello”; 14 String s = “good bye”; 15 char charArray[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ }; 16 boolean b = true; 17 char c = ‘K’; 18 int i = 7; 19 long l = 10000000; 20 float f = 2.5f; 21 double d = 33.333; 22 StringBuffer buffer = new StringBuffer(); 23 24 buffer.insert( 0, o ); 25 buffer.insert( 0, ” ” ); 26 buffer.insert( 0, s ); 27 buffer.insert( 0, ” ” ); 28 buffer.insert( 0, charArray ); 29 buffer.insert( 0, ” ” ); 30 buffer.insert( 0, b ); 31 buffer.insert( 0, ” ” ); 32 buffer.insert( 0, c ); 33 buffer.insert( 0, ” ” ); 34 buffer.insert( 0, i ); 35 buffer.insert( 0, ” ” ); 36 buffer.insert( 0, l ); 37 buffer.insert( 0, ” ” ); 38 buffer.insert( 0, f ); 39 buffer.insert( 0, ” ” ); 40 buffer.insert( 0, d ); 41 42 String output = 43 “buffer after inserts:n” + buffer.toString(); 44 Fig. 10.16 StringBufferclass insertand deletemethods (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply