• 将含有逻辑运算符的字符串解析为逻辑符号


    import javax.script.*;
    public class TestScript {
        public static void main(String[] args) throws Exception {
            String str = "(a >= 0 && a <= 5)";
              System.out.println(TestScript.isInclude(str,str));
     
        }
          public static Boolean isInclude(String elValue,String elString){ 
          String el = elString.substring(elString.indexOf("(")+1, elString.indexOf(")")); 
          

          System.out.println(el);
          ScriptEngineManager manager = new ScriptEngineManager();  //ScriptEngineManager  和  ScriptEngine   
          ScriptEngine engine = manager.getEngineByName("js");  //ScriptEngine engine = ScriptEngineManager.getEngineByName("js") ;
          engine.put("money",elValue);
          boolean eval = false;
          try {
            eval = (boolean) engine.eval(el);
          } catch (ScriptException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          return eval;
        }

    }
     
    使用ScriptEngine 对象的eval方法解析逻辑字符串
     
  • 相关阅读:
    使用JS对字符串进行MD5加密
    awk 控制语句if-else
    nginx日志切割
    Nginx日志监控工具
    运维小工具
    shell脚本2
    nginx优化方案
    centos7修复grub2
    Centos7的引导顺序
    如何同时运行两个tomcat?
  • 原文地址:https://www.cnblogs.com/lanqingzhou/p/8967329.html
Copyright © 2020-2023  润新知