Results 1 to 7 of 7
  1. #1
    Timmy is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default 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);

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    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.

  3. #3
    Timmy is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the reply.

    I put the result in a string now, however, the result is still the same with nothing being replaced.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    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("\\^", " "));
      }
    }

  5. #5
    Timmy is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you very much!

    This did indeed fix the problem.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    If you don't need the power of a regex, just use String#replace(...). Yes, it does replace all occurrences.

    db

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Quote Originally Posted by Toll View Post
    The string you're looking for is a regex, and ^ is one of the control characters when using regex (I believe negation).
    Outside a character class, the beginning of the input.

    As the first character in a character class, negation.

    db

Similar Threads

  1. Replies: 2
    Last Post: 04-25-2011, 05:13 PM
  2. replaceAll Problem
    By steve_m in forum New To Java
    Replies: 3
    Last Post: 12-22-2010, 02:09 PM
  3. Regular Expressions and String.replaceAll()
    By meta1203 in forum New To Java
    Replies: 1
    Last Post: 11-25-2010, 12:41 AM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 06:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •