[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1521
  • Last Modified:

Replace all in StringBuffer

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?
0
alice20
Asked:
alice20
  • 3
2 Solutions
 
CEHJCommented:
You need to turn it into a String first
0
 
CEHJCommented:
(that is if you don't want to use http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#matcher(java.lang.CharSequence) )

Use StringBuilder in preference to StringBuffer where you can
0
 
marchentCommented:
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, "") );
System.out.println(sb);

Open in new window

0
 
CEHJCommented:
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now