Re: How do I do a LOT of non-regular-
Stryder <stryder100@gmail.com> wrote:
I'm trying to do a lot (several hundred) of replacements in a string.
These are just string replacements I'm trying to do, not regular
expressions. Here's the WRONG way to do it, but hopefully it tells
you what it is I'm trying to do...
static String unescapeString(String string) {
Iterator i = entitiesHashMap.keySet().iterator();
for (String key : entitiesHashMap.keySet()) {
string = string.replaceAll(key,
(String) entitiesHashMap.get(key));
}
return string;
}
entitiesHashmap is a HashMap with literally hundreds of entries.
For just hundreds of elements in the map, your approach isn't all that
bad, except that it may re-replace results from earlier replacements.
If you know the lengths of the longest and shortest keys in the
HashMap, then you could check all feasible substrings in the
HashMap, like:
for (int startIdx=0; startIdx<string.length(); startIdx++) {
for (int len= lenLongestKey; len>=lenShortestKey; len--) {
String sub=string.substring(startIdx,len);
if (entitiesHashMap.count(sub) > 0) {
String replacementString=entitiesHashMap.get(sub);
string=string.substring(0,startIdx) +
replacementString +
string.substring(startIdx+len);
startIdx += replacementString.length()-1;
break;
}
}
}
If you don't know these min/max lengths, you can
1) obtain them from iterating the keySet(), or
2) you can let the inner loop always go
from string.length()-startIdx down to 1
If the strings are rather short, and the Map very very large (rather
in the range of hundreds of thousands, than just hundreds) and you
also don't want to maintain these min/max lengths together with the
map, then "2)" wins.
PS: you can optimize away the .count(sub), by just checking the
replacementString for null.