I'm currently building a config file that will replace a given string with multiple variables, I'm having a hard time explaining it so perhaps it would be best to show you what I mean:
The following method will be used with the String: Hello ~ and welcome to ~!
private static String REPLACE_CHAR = "~";
public static String parse(String key, String... inputs) {
int cache = matchCache.get(key, -1);
String value = customConfig.getString(key);
if (cache == -1) {
cache = StringUtils.countMatches(value, REPLACE_CHAR);
matchCache.put(key, cache);
}
for (int i = 0; i < cache; i++) {
value = value.replaceFirst(REPLACE_CHAR, inputs[i]);
}
return value;
}
what would be the fastest way to replace ~ with the first input, then move to the second ~ and so on...
Now the main reason I haven't used someone else's code: Ideally I'd like to create a List of different replaceable characters which will in turn replace the variable automatically, so in this same code I'm able to give it no inputs but it will check the List for replaceable characters and perform a method such as getYourName() This list does not need to be checked every time so a pattern may still be compiled?
I'm doing this to learn as much as for efficiency, I lack in ability when it comes to regex!