【发布时间】:2017-03-19 18:45:42
【问题描述】:
我正在尝试使用 Apache Commons Lang 的 StrSubstitutor 替换仅使用前缀标记的字符串中的变量,例如就像在 SQL 查询中用: 标记的命名参数一样。
这是我正在使用的代码 sn-p,它不起作用。
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.text.StrMatcher;
import org.apache.commons.lang3.text.StrSubstitutor;
Map<String,String> m = ImmutableMap.of("a", 1);
StrSubstitutor strSubstitutor = new StrSubstitutor(m)
.setVariablePrefix(":")
.setVariableSuffix("");
System.out.println(strSubstitutor.replace("select a from t where a = :a"));
// expect select a from t where a = 1
知道怎么做吗?
我正在尝试实现自定义StrMatcher,但仍然不成功。
有谁做过,可以分享一下经验吗?
【问题讨论】:
标签: java string apache-commons-lang apache-commons-lang3