tags:

views:

135

answers:

2

I found String.replaceAll() in Java with regular expression underlying. It works fine in short string. But in case of long string, I need a more efficient algorithm instead of String.replaceAll(). Anyone could give advice? Thanks!

A: 

You could try String.replace(CharSequence target, CharSequence replacement). It still uses a pattern and a matcher but target is not a regular expression.

Andreas_D
Precisely because this uses a `Pattern`/`Macher` underneath, it's wrong to think that this is a lot more efficient performance wise (though it should be acceptable).
polygenelubricants
+2  A: 

If you want to do incremental replacement, you can use an explicit appendReplacement/Tail loop to a StringBuffer (unfortunately no StringBuilder overloads as of yet).

Here's the idiom from the documentation:

 Pattern p = Pattern.compile(PATTERN);
 Matcher m = p.matcher(INPUT_SOURCE);

 StringBuffer sb = new StringBuffer();
 while (m.find()) {
     m.appendReplacement(sb, REPLACEMENT);
 }
 m.appendTail(sb);

 System.out.println(sb.toString());

This is pretty much how replaceAll is implemented.

The benefit of this method is that since you have full control over the replacement iteration, you don't have to store the entire output in memory at any given time as a potentially long String. You can incrementally build the output, flushing the StringBuffer content to the disk periodically. That is, using this method can be more memory efficient than using replaceAll.

(You can also do fancy replacements that is not supported by the current replacement syntax, e.g. toUpperCase() transformation).

Note that there is a request for enhancement for Matcher to be able to append to any Appendable. If granted, not only can you use StringBuilder, but you can also directly replace to e.g. a FileWriter.

Related questions

See also

polygenelubricants