Possible Duplicate:
Why can’t I Remove WhiteSpaces in Java?
In a java program, to make some XML parseable without giving errors, I am substituting the "&" character with "&" using the following code– the variable ‘origString’ is storing each line in the input file (one by one)
if(origString.contains("&"))
{
System.out.println(" This line contains & --");
System.out.println(" origstring=" + origString);
origString.replace("&" , "&");
System.out.println(" origstring after replacing '&' =" + origString);
}
However, when I run the above code, I am getting the same string value unchanged- i.e. the “&” remains as it is in the origString variable.
What am I doing wrong here? How do I correct this error?
Also, will the same problem apply to replacement of “<” , “>” , “””, “‘” characters (with equivalent text that is acceptable in XML files)?
Stringare immutable so you need to assign it back toStringvariable to actually make changes in that variabletry this:
and check in same way if you are doing same thing for replacement of “<” , “>” , “””, “‘” characters