Problem with replaceAll
Hi, I'm having a problem with the replaceAll function. I'm trying to find and replace a certain substring in the String "functieX0". However, Java does not seem to recognise "^". Anybody knows how to fix this?
Part of the code:
search = (getal1 + "^" + getal2);
functieX0.replaceAll(search, "math.pow("+getal1 +"," + getal2 +")");
Part of the problem could be that replaceAll returns a String, it does not alter the String that it was called on. So in that example, functieX0 is the same both before and after the call, no matter if something was found to replace or not.
Thanks for the reply.
I put the result in a string now, however, the result is still the same with nothing being replaced.
The string you're looking for is a regex, and ^ is one of the control characters when using regex (I believe negation). Put a \\ before the ^ and it should work. Here's a piece of quick code to compare to (this replaces the ^ with a space):
public static void main(String args)
String toReplace="This should^be replaced";
System.out.println(toReplace.replaceAll("\\^", " "));
Thank you very much!
This did indeed fix the problem. :(y):
If you don't need the power of a regex, just use String#replace(...). Yes, it does replace all occurrences.
Outside a character class, the beginning of the input.
Originally Posted by Toll
As the first character in a character class, negation.