我現在有個String a = "row == 1 || row == 2"字符串的長度是不確定的  但是可以組成這樣一個類似的String字符串,我想用這個當作if的判斷條件   該怎么作?   
就是要形成這樣的型式
if(row == 1 || row == 2){
……
}先謝謝了哈

解决方案 »

  1.   

    String的可以转成boolean吗?我也学习下。
      

  2.   

    String a = Boolean.toString(row == 1 || row == 2);
      

  3.   

    用MVEL包.这个相当于javascript中的EVAL
      

  4.   


    嗯   Razer~~
      

  5.   

    He wants to transfer String to boolean...
      

  6.   

    那 
    if(a){
    } ?
    不还是一个STRING吗。
    如果
    if(Boolean.toString(row == 1 || row == 2)){
    }
    那不是多此一举?
    意义何在?
      

  7.   


    Right, 
    crazy
    I've no idea 了~
      

  8.   

    这个目前的 JDK 是没有这个能力的!可以试着用用 Java 编写的动态语言下载 BeanShell 动态语言 http://www.beanshell.org/import bsh.EvalError;
    import bsh.Interpreter;public class Test1 {  
        
        public static void main(String[] args) throws EvalError {
            int row = 2;
            String a = "row == 1 || row == 2";        boolean result = false;
            Interpreter bsh = new Interpreter();
            bsh.set("row", row);
            bsh.eval("result = " + a);
            result = ((Boolean)bsh.get("result")).booleanValue();        if(result) {
                System.out.println("true");
            } else {
                System.out.println("false");
            }
        }
    }row 的值是事先知道的,使用 Interpreter#set 方法设置一下就行了。
      

  9.   

    if(Boolean.parseBoolean(String s) )
      

  10.   

    4 楼说的 MVEL 也是类似功能的http://mvel.codehaus.org/
      

  11.   

    改成这样更好一些:import bsh.EvalError;
    import bsh.Interpreter;public class Test1 {  
        
        public static void main(String[] args) throws EvalError {
            int row = 2;
            String a = "row == 1 || row == 2";                
            Interpreter bsh = new Interpreter();
            bsh.set("row", row);
            boolean result = ((Boolean)bsh.eval(a)).booleanValue();
            if(result) {
                System.out.println("true");
            } else {
                System.out.println("false");
            }
        }
    }