Results 1 to 7 of 7
Thread: Problem with replaceAll
- 06-06-2011, 11:33 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
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);
System.out.println(search);
functieX0.replaceAll(search, "math.pow("+getal1 +"," + getal2 +")");
System.out.println(functieX0);
- 06-06-2011, 12:36 PM #2
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.
- 06-06-2011, 02:49 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
Thanks for the reply.
I put the result in a string now, however, the result is still the same with nothing being replaced.
- 06-06-2011, 02:56 PM #4
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):
Java Code:class ReplaceTest { public static void main(String[] args) { String toReplace="This should^be replaced"; System.out.println(toReplace.replaceAll("\\^", " ")); } }
- 06-06-2011, 03:41 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
Thank you very much!
This did indeed fix the problem.
- 06-06-2011, 04:49 PM #6
If you don't need the power of a regex, just use String#replace(...). Yes, it does replace all occurrences.
db
- 06-06-2011, 04:51 PM #7
Similar Threads
-
String.replaceAll(str, str) is leading to infinite loop. Help?
By TheNadz in forum New To JavaReplies: 2Last Post: 04-25-2011, 04:13 PM -
replaceAll Problem
By steve_m in forum New To JavaReplies: 3Last Post: 12-22-2010, 01:09 PM -
Regular Expressions and String.replaceAll()
By meta1203 in forum New To JavaReplies: 1Last Post: 11-24-2010, 11:41 PM -
replaceALL(char oldChar, char newChar) method
By arson09 in forum New To JavaReplies: 0Last Post: 04-28-2010, 05:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks