Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Replace Phrase in String

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Replace Phrase in String

    Ok, should be a fairly simple thing to do, but it doesn't seem to have a method in the API.

    If I have a String like this:
    "I go to [WEBSITE] all the time."

    I want to be able to replace the phrase [WEBSITE] with something I specify. That phrase could appear several times in the String, and I want to replace it with the same value every time it appears.

    I know the String class as a replace method, but it only accepts chars, while I want to replace a phrase instead. My current code just finds the phrase, substrings it out, and replaces it with something else. I feel there has to be a better way.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Replace Phrase in String

    Did you even look at the String API?

    String (Java Platform SE 6)

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Replace Phrase in String

    Well, clearly an outdated one...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. How can I convert a String to Set<String>? Is it possible?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 25th, 2010, 09:03 AM
  2. Replies: 6
    Last Post: May 25th, 2010, 02:15 AM
  3. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 11:16 AM
  4. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM