/*
 * @topic T10030 StringBuilder replace substrings
 * @brief main driver program
*/
package week02;

public class MainApp {
    public static void main(String[] args) {
        // Strings are immutable, we must make a copy:
        String greeting = new String( "hello hello hello" );
        greeting = greeting.replace( "o", "A" );
        System.out.println( greeting );

        // With StringBuilder we can make changes in place:
        StringBuilder greeting = new StringBuilder( "hello hello hello" );
        stringBuilderReplaceAll( greeting, "lo", "AAAA" );
        System.out.println( greeting );
    }//main
    
    public static void stringBuilderReplaceAll(
            StringBuilder str, String findWhat, String replaceWith )
    {
        for (;;) {
            int from = str.indexOf( findWhat );
            if ( from == -1 ) {
                // no more occurences of findWhat were found:
                break;
            }
            int to = from + findWhat.length();
            str.replace( from, to, replaceWith );
        }
    }//void stringBuilderReplaceAll

}//class MainApp