• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JText Area..searching and replacing text

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Marshal
    Posts: 80634
    471
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    . . . and StringBuilder appears to have replace method too.
     
    Ben Jordan
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Sheriff
    Posts: 28395
    100
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so something like this,
     
    Campbell Ritchie
    Marshal
    Posts: 80634
    471
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    this works...I spent a lot time working on this one last night,,,
     
    Campbell Ritchie
    Marshal
    Posts: 80634
    471
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done getting it to work.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic