Results 1 to 13 of 13
  1. #1
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default replace string, what is faster

    Hi
    in an application I use string replace method to replace some substring as follow:
    myString.replaceAll("k5m6m7k4m3","");
    The strings are sometime very long.
    My question, is the replaceAll the best way and fast enough, or is there another way to do it?
    regards

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    Premature optimization makes kittens cry.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by henry123 View Post
    Hi
    in an application I use string replace method to replace some substring as follow:
    myString.replaceAll("k5m6m7k4m3","");
    The strings are sometime very long.
    My question, is the replaceAll the best way and fast enough, or is there another way to do it?
    regards
    It's certainly the easiest. Is it unacceptably slow for your purposes?

  4. #4
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    Is it unacceptably slow for your purposes?
    I can not say, if it is slow, because I see no other posbility to avoid it!! But as I said the strings are big and long. They are actually text files and somethimes could be few MBs big. I dont know other possibility to replaceAll in JAVA?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    replaceAll() is using a regex. You could try replace() since no regex is involved in the example you gave.

    An illustration of the problem would be good. For instance showing the slowness to be with the replace operation rather than IO.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Something like this.

    Java Code:
    String.replaceAll("it(?! this)", "it is ball")

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Quote Originally Posted by henry123 View Post
    I can not say, if it is slow, because I see no other posbility to avoid it!! But as I said the strings are big and long. They are actually text files and somethimes could be few MBs big. I dont know other possibility to replaceAll in JAVA?
    I wouldn't read the whole thing in in the first place.
    Read a line, do a replaceAll(), write the line.
    Repeat til end of file.

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    the problem is not the method replaceAll but using a string when other types are more appropriate, in this case the appropriate type could be a StringBuffer.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    That's true. But OP asking more relevant to the replaceAll() method, is there any alternatives to use, isn't it ...

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    But there's nothing to indicate a StringBuffer would help is there?
    We don't know what the OP is doing with the transformed text yet...

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 04:58 PM
  2. replace a string using Text i/o
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 07-06-2009, 03:38 AM
  3. string replace problem
    By soni in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2008, 02:21 AM
  4. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 06:51 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 09:14 PM

Posting Permissions

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