您可以使用mvel2表达式来判断一个字符串是否包含一个字符,不区分大小写。以下是一个示例代码:

import org.mvel2.MVEL;

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        char ch = 'o';

        String expression = "str.toLowerCase().contains(ch.toString().toLowerCase())";
        Boolean result = (Boolean) MVEL.eval(expression, createVariables(str, ch));

        System.out.println(result);
    }

    private static Object createVariables(String str, char ch) {
        return MVEL.createVariableMap("str", str, "ch", ch);
    }
}

在上面的代码中,我们首先定义了一个字符串str和一个字符ch。然后,我们使用MVEL.eval()方法来评估一个mvel2表达式。该表达式首先将字符串转换为小写,并使用contains()方法来检查是否包含指定的字符。在表达式中,我们使用了toLowerCase()方法来忽略大小写。

最后,我们打印出结果。如果字符串包含字符,则结果为true,否则为false

请注意,为了在mvel2表达式中使用变量,我们需要使用MVEL.createVariableMap()方法创建一个变量映射。在上面的代码中,我们使用了createVariables()方法来创建变量映射

判断一个字符串是否包含一个字符不区分大小写用mvel2表达式书写

原文地址: https://www.cveoy.top/t/topic/iagi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录