I am writing a word processor for a class and I need to search the area after th
ID: 3629850 • Letter: I
Question
I am writing a word processor for a class and I need to search the area after the current cursor position and find a specific word the user enters and replace it with another word also given by the user. So, I have a JTextArea filled with text. I find the cursor position in this JTextArea (lets call it textArea), and I substring the textArea from cursorPos to the end of textArea. This substring (call it searchArea) is the search area because it is the area after the cursor. Now, I simply check to see if the text the user wants to replace (from) is in the searchArea, and if it is, the user must input in which word they want to use to replace from with (to), and then to will replace from. My search algorithm for searchArea works fine, it returns true if it finds the word in the searchArea, else returns false. However, I'm having trouble replacing from with to. There is a method that JTextArea has called replaceRange. but I'm not sure of which parameters to plug in. I know that the String to use for replacing is to, but I'm not sure about the beginning and ending of the word..Any help?
Explanation / Answer
You can do a replaceAll() on searchArea as long as you update the String stored in the JTextArea with the modified version.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.