I have a class that processes a lot of text. For each line whose length is between 100 and 2000 characters, I execute 30 different line notes.
Example:
string modified;
for(int i = 0; i < num_strings; i++){
modified = runReplacements(strs[i]);
}
public runReplacements(String str){
str = str.replace("foo","bar");
str = str.replace("baz","beef");
....
return str;
}
'foo', 'baz' and all other "targets" should appear only once and are string literals (there is no need for a real regular expression).
As you can imagine, performance bothers me :)
Considering this,
replaceFirst()seems like a bad choice because he will not use it Pattern.LITERALand will do additional processing that is not required.
replace() seems like a bad choice because it will traverse the entire line looking for multiple instances that need to be replaced.
, , , , String.replaceFirst() String.replace() Pattern.compile . , , :
, ( ), regex, .
, ?