cancel
Showing results for 
Search instead for 
Did you mean: 

String replace

Former Member
0 Kudos

Hi,

I have an XML message as String. I need to replace

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

with space

blank. How to do that on Java 1.4.2

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello,

have a look at [this|http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceFirst(java.lang.String,%20java.lang.String)] method

regrads franz

...close thread if message is answered

Former Member
0 Kudos

unfortunatly, ther are many occurrancess of this string

Former Member
0 Kudos

then have a look at the other replace methods in java.lang.String

regards franz

Former Member
0 Kudos

well I did it with this part of code:

	public static String replaceStr(String source, String pattern, String replace) {
		if (source != null) {
			final int len = pattern.length();
			StringBuffer sb = new StringBuffer();
			int found = -1;
			int start = 0;
			while ((found = source.indexOf(pattern, start)) != -1) {
				sb.append(source.substring(start, found));
				sb.append(replace);
				start = found + len;
			}
			sb.append(source.substring(start));
			return sb.toString();
		} else
			return "";
	}

But there can be problem replacing in huge messages? How to speedUp something like that?

Former Member
0 Kudos

why not use String.replaceAll (regular expression) - should be fast

looks like this


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

pattern looks a bit wired because of the many escape characters

regards franz

...close thread if message is answered

Answers (0)