Implementing the basic search and replace functionality of a text editor in java