StringFind.java
/// Section: Section 4.4.5 -- Searching and Replacing class StringFind { public static void main( String[] args ) { StringBuffer strbuf = new StringBuffer( "one hello is like any other hello" ); String searchString = "hello"; String replacementString = "armadillo"; int pos = 0; while ( ( pos = (new String(strbuf)).indexOf( searchString, pos ) ) != -1 ) { strbuf.replace( pos, pos + searchString.length(), replacementString ); pos++; } System.out.println( strbuf ); } }
Maintained by John Loomis, updated Mon Jan 01 17:11:31 2007