Replace all occurrences of substring in a string - which is more efficient in Java?

前端 未结 5 558
轮回少年
轮回少年 2020-12-15 09:53

I know of two ways of replacing all occurrences of substring in a string.

The regex way (assuming \"substring-to-be-replaced\" doesn\'t include regex specia

相关标签:
5条回答
  • 2020-12-15 10:22

    Here's the source code from openjdk:

    public String replace(CharSequence target, CharSequence replacement) {
        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
           this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
    }
    
    0 讨论(0)
  • 2020-12-15 10:22

    Not having done any profiling or benchmarking, I'd say it's a fairly safe bet that if you don't need regex magic, then the overhead of the regular expression parser (which you'll get no matter what, in terms of memory as well as CPU usage) costs you a lot more than you can possibly gain on the other end.

    0 讨论(0)
  • 2020-12-15 10:28

    Shouldn't you compare replaceAll 2 times? However, for a single invocation it will hardly be measurable. And will you do millions of comparisions?

    Then I would expect 'compile' to be faster, but only, if you don't use a constant String without any pattern-rules.

    Where is the problem in writing a micro benchmark? Or look up the source.

    0 讨论(0)
  • 2020-12-15 10:40

    String.replace() uses regex underneath.

    public String replace(CharSequence target, CharSequence replacement) {
          return Pattern.compile(target.toString(), Pattern.LITERAL)
                 .matcher(this ).replaceAll(
                   Matcher.quoteReplacement(replacement.toString()));
      }
    

    Are there more efficient ways than the above described two?

    There are given that you operate on an implementation backed e.g., by an array, rather than the immutable String class (since string.replace creates a new string on each invocation). See for instance StringBuilder.replace().

    Compiling a regex incurs quite alot of overhead which is clear when observing the Pattern source code. Luckily, Apache offers an alternative approach in StringUtils.replace() which according to the source code (line #3732) is quite efficient.

    0 讨论(0)
  • 2020-12-15 10:40

    Instead of using strings, which are immutable, use char arrays or some other mutable type (such as StringBuffer or StringBuilder).

    0 讨论(0)
提交回复
热议问题