replace period using String.replaceAll()

Discussion in 'Java' started by sfu, Sep 24, 2003.

  1. sfu

    sfu Guest

    Hi

    i'm trying to remove all occurances of . from a string

    test = test.replaceAll(".","");

    and

    int fs = 46;
    Character fullstop = new Character((char) fs);
    test = test.replaceAll(fullstop.toString(),"");

    both dont work, any ideas on how to get it to work?

    cheers
     
    sfu, Sep 24, 2003
    #1
    1. Advertising

  2. sfu

    secret Guest

    It appears that your problem is that the period has a defined meaning within
    regular expression. This is not replacing a substring in the way you
    imagine.

    Try putting two backslashes in front of the period. One backslash is needed
    to escape the char in the regular expression. The other is needed to escape
    the first backslash in Java.

    test = test.replaceAll("\\.","");


    "sfu" <> wrote in message
    news:...
    > Hi
    >
    > i'm trying to remove all occurances of . from a string
    >
    > test = test.replaceAll(".","");
    >
    > and
    >
    > int fs = 46;
    > Character fullstop = new Character((char) fs);
    > test = test.replaceAll(fullstop.toString(),"");
    >
    > both dont work, any ideas on how to get it to work?
    >
    > cheers
     
    secret, Sep 25, 2003
    #2
    1. Advertising

  3. sfu

    secret Guest

    I just reread my post and decided it was quite unitelligible (though the
    code works).

    The essence of your problem is that the method replaceAll in String does not
    replace a substring with the replacement string the way you might
    intuitively imagine. Chech the API and you'll see that the first argument
    is a regular expression, not a substring of the target.

    good luck

    alan

    "sfu" <> wrote in message
    news:...
    > Hi
    >
    > i'm trying to remove all occurances of . from a string
    >
    > test = test.replaceAll(".","");
    >
    > and
    >
    > int fs = 46;
    > Character fullstop = new Character((char) fs);
    > test = test.replaceAll(fullstop.toString(),"");
    >
    > both dont work, any ideas on how to get it to work?
    >
    > cheers
     
    secret, Sep 25, 2003
    #3
  4. sfu

    Chris Smith Guest

    secret wrote:
    > I just reread my post and decided it was quite unitelligible (though the
    > code works).
    >
    > The essence of your problem is that the method replaceAll in String does not
    > replace a substring with the replacement string the way you might
    > intuitively imagine. Chech the API and you'll see that the first argument
    > is a regular expression, not a substring of the target.



    The part that's far less clear from the documentation is that the second
    parameter isn't just a String either; it's a pseudo-regexp where most
    special characters don't work, but subexpression references do. That
    means, essentially, that you have to be sure to properly escape BOTH
    parameters to this method (and in consequence, the method is practically
    useless unless both parameter values are known when the code is
    written).

    In the OP's case, that's not a problem, though. Just a gotcha to look
    out for.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Sep 25, 2003
    #4
  5. sfu

    Roedy Green Guest

    On 24 Sep 2003 15:53:08 -0700, (sfu) wrote or
    quoted :

    >test = test.replaceAll(".","");


    replaceAll is a regex method masquerading as a humble String method.

    .. has magic meaning in regexs.

    See http://mindprod.com/jgloss/regex.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 25, 2003
    #5
  6. sfu

    D Goldman Guest

    Hello, try test = test.replaceAll("[.]","");

    Hope this helps,
    Daniel Goldman
    http://d-goldman.org


    (sfu) wrote in message news:<>...
    > Hi
    >
    > i'm trying to remove all occurances of . from a string
    >
    > test = test.replaceAll(".","");
    >
    > and
    >
    > int fs = 46;
    > Character fullstop = new Character((char) fs);
    > test = test.replaceAll(fullstop.toString(),"");
    >
    > both dont work, any ideas on how to get it to work?
    >
    > cheers
     
    D Goldman, Sep 25, 2003
    #6
  7. sfu

    sfu Guest

    "secret" <> wrote in message news:<qLpcb.9213$>...
    > It appears that your problem is that the period has a defined meaning within
    > regular expression. This is not replacing a substring in the way you
    > imagine.
    >
    > Try putting two backslashes in front of the period. One backslash is needed
    > to escape the char in the regular expression. The other is needed to escape
    > the first backslash in Java.
    >
    > test = test.replaceAll("\\.","");


    cheers mate!
     
    sfu, Sep 25, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    745
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,638
    Mladen Adamovic
    Dec 5, 2003
  3. Peebster_99
    Replies:
    5
    Views:
    18,882
    Mladen Adamovic
    Mar 7, 2004
  4. William Krick
    Replies:
    4
    Views:
    77,874
    rt19983
    Dec 18, 2008
  5. Tim

    String replaceAll fails

    Tim, Dec 22, 2004, in forum: Java
    Replies:
    5
    Views:
    4,243
    Josh Martin
    Dec 29, 2004
Loading...

Share This Page