如何使用不同的分隔符进行插值查找?

问题描述 投票:0回答:1

我使用 Apache Commons Configuration 来加载配置文件。过去,configs

getProperty()
方法被覆盖,以便属性值中的
${encoded 21$%Ds}
作为解码值返回。现在应该改进这一点。

我知道用冒号分隔

${encoded:21$%Ds}
我可以通过设置对配置插值器的查找来轻松检索和自动解码该值(1):

ConfigurationInterpolator interpolator = config.getInterpolator();
interpolator.registerLookup("encoded", new DecodeLookup());

DecodeLookup
看起来像

public class BogisLookup implements Lookup {
  @Override
  public Object lookup(String varName) {
    String decodedVar = decode(varName);
    return decodedVar;
    }
}

由于带有空格(

${encoded 21$%Ds}
)的版本已经被广泛采用,我需要将插值查找分隔符从
":"
调整为
" "
。我该怎么做?

java configuration apache-commons apache-commons-config
1个回答
0
投票

通过正则表达式处理数据:

String[] parts = varName.split("\\s+");

这样就可以得到一个数组,其中索引为0的数据为Key,索引为1的数据为Value。如果长度超过2,就会抛出错误。当长度验证通过后,可以直接得到索引为1的数据进行解码

© www.soinside.com 2019 - 2024. All rights reserved.