tags:

views:

45

answers:

1

Im trying to create a xml file from a POJO , in which i have a property that stores urls, I have been using the below method to replace all & in the url String to make the xml conform to standards and pass it as an html char entity but the string does not change.

public static String forHrefAmpersand(String aURL){
    return aURL.replaceAll("&", "&");
}

the value might be www.abc.com/controller?a=1&next=showResults

I have even tried changing the above method to use "/" as i read replaceAll uses regular expression but replaceAll is not working as expected, Can anyone tell me what is the mistake im doing ? Thanks in advance

A: 

There is probably a bug somewhere else in your code. I suggest to print the string which forHrefAmpersand() returns to System.out. It should contain the value which you expect.

PS: If you're using Java 5, you can use String.replace() instead.

Aaron Digulla
John
after googling it seems lot of people had faced the same issuepublic String replaceAll(String regex, String replacement)This method works fine if replacement string does not contain ‘$’ or ‘/’ characters.If replacement string contains these characters, the results can be in-accurate.Fix: Use Matcher.quoteReplacement(replacement)public String replaceAll(String regex, Matcher.quoteReplacement(replacement))
John
`replace()` also replaces all occurrences but doesn't use regexp, so there are no special characters.
Aaron Digulla
will try both thanks Aaron
John
Aaron Digulla