MVEL2表达式实现字符串字符替换:将'a'替换成'b'
在MVEL2表达式中,可以使用replace()函数来实现将字符串中的某个字符替换成另一个字符的功能。下面是一个示例:
import org.mvel2.MVEL;
String input = 'Hello, world!';
String expression = "input.replace('a', 'b')";
String output = (String) MVEL.eval(expression, new HashMap<String, Object>() { {
put('input', input);
}});
System.out.println(output);
输出结果为:
Hellb, wbrld!
在上面的示例中,我们使用replace()函数将字符串中的字母'o'替换成字母'b'。在MVEL2表达式中,可以使用类似的方式将字符串中的'a'字符替换成'b'字符。
原文地址: https://www.cveoy.top/t/topic/fK5p 著作权归作者所有。请勿转载和采集!