Java Replace Special Characters

I need to remove this in a Java string:

<?xml version="1.0" encoding="UTF-8"?>

I'm receiving badly formatted xml on a queue that has this tag embedded in the middle of the xml, because of some kind of concatenation on the sender's side.

I've tried:

String toReplace = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
String newString = oldString.replaceAll(toReplace, "");

Does anyone know how to handle this?
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
The replaceAll method takes in a regular expression defining the pattern of text to find. Your problem is that you are using characters which are special to regex and need to be escaped. Try:

String toReplace = "<\\?xml version=\"1\\.0\" encoding=\"UTF-8\"\\?>";
String newString = oldString.replaceAll(toReplace, "");

Open in new window, java.lang.String)
CEHJConnect With a Mentor Commented:
If you're not going to take advantage of the regex (say to allow for variations in whitespace, quoting etc.) then it will be easier to do

String newString = oldString.replace(toReplace, "");

Open in new window

The version i posted is also more efficient than using a regex
All Courses

From novice to tech pro — start learning today.