StringInsert.java
// This code example is from the following source: // // Book Title: Programming with Objects, A Comparative Presentation // of Object Oriented Programming with C++ and Java // // Chapter: Chapter 4 ---- Strings // // Section: Section 4.4.6 -- Erasing and Inserting Substrings // // The links to the rest of the code in this book are at // // http://programming-with-objects.com/pwocode.html // // For further information regarding the book, please visit // // http://programming-with-objects.com // // StringInsert.java class StringInsert { public static void main( String[] args ) { int pos = 0; StringBuffer quote = new StringBuffer( "Some cause happiness wherever they go," + " others whenever they go - Oscal Wilde" ); String search = "happiness"; if ( ( pos = ( new String(quote) ).indexOf( search) ) != -1 ) { quote.delete( pos, pos + search.length() ); quote.insert( pos, "excitement" ); } System.out.println( quote ); } }
Maintained by John Loomis, updated Mon Jan 01 17:11:39 2007