aspose file tools*
The moose likes Beginning Java and the fly likes JText Area..searching and replacing text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JText Area..searching and replacing text" Watch "JText Area..searching and replacing text" New topic
Author

JText Area..searching and replacing text

Ben Jordan
Greenhorn

Joined: Apr 19, 2007
Posts: 18


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

Joined: Oct 13, 2005
Posts: 39865
    
  28
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

    Joined: Oct 13, 2005
    Posts: 39865
        
      28
    . . . and StringBuilder appears to have replace method too.
    Ben Jordan
    Greenhorn

    Joined: Apr 19, 2007
    Posts: 18
    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18907
        
        8

    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

    Joined: Apr 19, 2007
    Posts: 18
    so something like this,
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39865
        
      28
    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

    Joined: Apr 19, 2007
    Posts: 18


    this works...I spent a lot time working on this one last night,,,
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39865
        
      28
    Well done getting it to work.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JText Area..searching and replacing text