Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JText Area..searching and replacing text

 
Ben Jordan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my code. I need to know how to serach the area for certain text and then be able to replace it. I know I will have to use the replace () method somewhere in there to replace the text but not sure on how to search for it.
ANy help youc ould provide woule be greatly appricated.

Thanks,
Ben Jordan
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

At least three ways on doing it

  • Basic: Use the String class which has indexOf() methods; you can repeatedly find a particular bit of text. it has replace() and replaceAll() method which creates a new String object. You doubtless already know you can't change a String.
  • Not quite so basic: Use the StringBuilder clasa (a more recent replacement for StringBuffer). It has methods for finding a particular index, and inserting or deleting text.
  • Maybe more advanced. Find the StrBuilder class in Jakarta Commons, which is similar to StringBuilder.
  • Fuller details in the API.
     
    Campbell Ritchie
    Sheriff
    Posts: 48415
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    . . . and StringBuilder appears to have replace method too.
     
    Ben Jordan
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell,
    Thanks for the help....I should clarifiy what I mean. How would the code be? would it something like this...


    Let me know....

    Thanks,
    Ben
     
    Paul Clapham
    Sheriff
    Pie
    Posts: 20758
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No. Here's what you should do.

    1. Get the text from the JTextArea into a String.

    2. Consider what the earlier posts said to do with String data.

    3. Put the modified String data back into the JTextArea.
     
    Ben Jordan
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    so something like this,
     
    Campbell Ritchie
    Sheriff
    Posts: 48415
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, 'fraid not.

    Your JTextArea isn't a String. You need to get the text from it, which is in the form of a string, then you can use a replace method, or put it into a StringBuilder and use that to manipulate it.
    Then you can put the text back into the text area.

    Look at the API description for JTextArea. Here.

    Alternatively there is one place in your coding where you already have the text in the form of a String.
     
    Ben Jordan
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    this works...I spent a lot time working on this one last night,,,
     
    Campbell Ritchie
    Sheriff
    Posts: 48415
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done getting it to work.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic