Created
October 14, 2008 10:21
-
-
Save redsquirrel/16695 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SymbolReplacer { | |
protected String stringToReplace; | |
SymbolReplacer(String s) { | |
this.stringToReplace = s; | |
} | |
String replace() { | |
Pattern symbolPattern = Pattern.compile("\\$([a-zA-Z]\\w*)"); | |
int startingPosition = 0; | |
while (true) { | |
Matcher symbolMatcher = | |
symbolPattern.matcher(stringToReplace.substring(startingPosition)); | |
if (symbolMatcher.find()) { | |
startingPosition += replaceSymbol(symbolMatcher); | |
} else | |
break; | |
} | |
return stringToReplace; | |
} | |
private int replaceSymbol(Matcher symbolMatcher) { | |
String symbolName = symbolMatcher.group(1); | |
if (getSymbol(symbolName) != null) | |
stringToReplace = stringToReplace.replace("$" + symbolName, | |
translate(symbolName)); | |
return symbolMatcher.start(1); | |
} | |
protected String translate(String symbolName) { | |
return getSymbol(symbolName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment