Results 1 to 5 of 5
  1. #1
    aham is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Java String Replace

    Hi,

    I've the following string

    String abc = "POST;DELETE;PUT;/user/testing/[0-9]*/"

    I wanna replace the string after the last ';' available in the above string.

    The option that I found is
    token.replaceAll("(.*)(;.*$)", "$1")

    Any better suggestion?

    Thanks
    Aham

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    If it will always be the last semi-colon then you can use lastIndexOf and substring.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    (untested)
    Java Code:
    replaceAll(";[^;]*$", "")
    Always provided I correctly interpreted
    I wanna replace the string after the last ';' available in the above string.
    which isn't adequately clear.

    When seeking help with regexes, it's always better to post a few examples of input and desired output.

    db

  4. #4
    aham is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks, I like the replaceAll(";[^;]*$", "") option.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    If your question is answered, please mark the thread as "solved" using the Thread Tools link near the top of the page.

    db

Similar Threads

  1. replace string, what is faster
    By henry123 in forum Advanced Java
    Replies: 12
    Last Post: 03-02-2011, 09:57 AM
  2. Replies: 9
    Last Post: 08-31-2010, 05:21 AM
  3. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 03:58 PM
  4. [SOLVED] how to replace exact string in java
    By pankaj_salwan in forum New To Java
    Replies: 22
    Last Post: 07-08-2008, 09:28 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM

Tags for this Thread

Posting Permissions

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