SearchAndReplace.java


Below is the syntax highlighted version of SearchAndReplace.java from §7.2 Regular Expressions.


/*************************************************************************
 *  Compilation:  javac SearchAndReplace.java In.java
 *  Execution:    java SearchAndReplace from to < input.txt
 *  
 *  Replaces all occurrence of the "from" string with the "to"
 *  string.
 *
 *************************************************************************/

public class SearchAndReplace { 


    public static void main(String[] args) { 
        String from = args[0];
        String to   = args[1];

        // read input
        In in = new In();
        String input  = in.readAll();

        // create and print output
        String output = input.replaceAll(from, to);
        System.out.println(output);
    }

}


Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:20:16 EST 2011.