Replace all in StringBuffer

Posted on 2008-11-14
Last Modified: 2012-05-05
I have a StringBuffer object, in the following value is repeated at several places.
<?xml version="1.0" encoding="UTF-8"?>
I want to replace all these values with a blank. I can't see a replaceAll function in StringBuffer. Can you please let me know how to do this?
Question by:alice20
    LVL 86

    Assisted Solution

    You need to turn it into a String first
    LVL 86

    Expert Comment

    (that is if you don't want to use )

    Use StringBuilder in preference to StringBuffer where you can
    LVL 13

    Accepted Solution

    what about convert the string buffer into string, the perform replaceAll, and then return back to string buffer?
    StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>blablablabaa<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    String replaceStr = "<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>";
    sb = new StringBuffer( sb.toString().replaceAll(replaceStr, "") );

    Open in new window

    LVL 86

    Expert Comment


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    post4 challenge 28 70
    MaxSpan challenge 9 50
    bunnyEars2 challenge 6 46
    wordlen challenge 3 29
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now