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 4 of 4

Thread: StringBuffer Problem

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default StringBuffer Problem

    I have user input in a StringBuffer. I need to search for the word "the" and replace every occurrence with another string, "zee." How do I do this?

    int theStart = sentence.indexOf("the");
    		int theEnd = theStart + "the".length();
    		sentence.replace(theStart, theEnd, "zee");

    What I have only works for the FIRST occurrence.
    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: StringBuffer Problem

    only works for the FIRST occurrence.
    Put the code in a loop and have the indexOf method start each search past the changed location.
    Or read the API doc for the String class to see if it has other useful methods.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: StringBuffer Problem

    take a peek at java.util.regex?

    edit:
    Nevermind, follow Norm's advice and look at the Java String API
    Last edited by JJeng; May 30th, 2012 at 12:33 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: StringBuffer Problem

    If the sentence variable is a StringBuffer, then you'll have to use its methods.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to I convert this Sentence String to StringBuffer?
    By iCitationNeeded in forum Java Theory & Questions
    Replies: 4
    Last Post: May 22nd, 2012, 11:45 AM
  2. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  3. StringBuffer Implemenatation
    By tcstcs in forum Java Theory & Questions
    Replies: 5
    Last Post: May 5th, 2011, 01:44 PM
  4. String Vs StringBuffer
    By kalees in forum Java SE APIs
    Replies: 5
    Last Post: November 6th, 2009, 03:27 AM