Функция что заменяет символы без их дублирования?
Мне нужно создать функцию, что заменяет символы в тексте.
Если создать unit test, то вот результат должен быть = bb cc cc.
Однако итог иной.
Assert.assertEquals("bb cc cc", substitute.replaceTextBy("aa bb cc", Arrays.asList("aa", "bb", "bb", "cc")));
Expected :bb cc cc
Actual :cc cc cc.
Вот моя функция. Советы?
@Override
public IOParams<?> evaluate(EvaluationEnvironement e) {
if (this.getChilds().size() < 3 || this.getChilds().size() % 2 == 0) {
throw new IllegalParameterException(
"Wrong number of parameters for " + getCaption() + " : " + this.getChilds().size() + ")");
} else if (this.getChilds().size() % 2 != 0) {
IOParams<?> params = this.getChildAt(0).evaluate(e);
List<String> textToReplace= new ArrayList<String>();
try {
for (int i = 1; i < this.getChilds().size(); i++){
IOParams<?> p = this.getChildAt(i).evaluate(e);
textToReplace.add(p.getStringResult());
}
} catch (CastNoneException e1) {
throw new IllegalParameterException(getCaption() + " atributte evaluated to NONE");
}
try {
return new StringParams(replaceTextBy( params.getStringResult(), textToReplace ));
} catch (CastNoneException e1) {
return NoneParams.getNone();
}
} else {
throw new IllegalParameterException("Wrong parameters number for " + getCaption() + " : " + this.getChilds().size());
}
}
public String replaceTextBy(String toConvert, List<String> substitutionPairs){
if (toConvert != null){
String res = toConvert;
for (int i = 1; i< substitutionPairs.size(); i += 2){
res = res.replace(substitutionPairs.get(i-1), substitutionPairs.get(i));
}
return res;
} else return null;
}