Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 无法将从输入对话框输入的字符串与Groovy中的其他字符串进行比较_String_Groovy_Compare_Gstring - Fatal编程技术网

String 无法将从输入对话框输入的字符串与Groovy中的其他字符串进行比较

String 无法将从输入对话框输入的字符串与Groovy中的其他字符串进行比较,string,groovy,compare,gstring,String,Groovy,Compare,Gstring,下面是我的代码: def readln = javax.swing.JOptionsPane.&showInputDialog def env = readln 'Which environment you want to test' 我以syst 当我比较时,这就是我正在做的 if("$env".equalsIgnoreCase("syst")){ some code } 也尝试了很多其他的方法来比较喜欢 if($env.equalsIgnoreCase("syst")) if(e

下面是我的代码:

def readln = javax.swing.JOptionsPane.&showInputDialog
def env = readln 'Which environment you want to test'
我以
syst

当我比较时,这就是我正在做的

if("$env".equalsIgnoreCase("syst")){
some code
}
也尝试了很多其他的方法来比较喜欢

if($env.equalsIgnoreCase("syst"))
if(env.equalsUIgnoreCase("syst"))
if("${'env'}".equalsIgnoreCase("syst"))

但上述工作条件均不满足。如何将声明的字符串与从对话框中输入的字符串进行比较?

首先尝试将其直接展开为一个字符串-“${env}”

-类名
JOptionPane
错误(它是
JOptionPane
-没有
s

下面是工作代码

您可以从groovy控制台运行它

import javax.swing.JOptionPane

def readln = JOptionPane.&showInputDialog
def env = readln 'Which environment you want to test'
if(env=='syst'){
    println "EQUALS"
}
if('syst'.equalsIgnoreCase(env)){
    println "EQUALS equalsIgnoreCase 1"
}
if(env.equalsIgnoreCase('syst')){
    println "EQUALS equalsIgnoreCase 2"
}
if("${env}".equalsIgnoreCase('syst')){
    println "EQUALS equalsIgnoreCase 3"
}
所有4个比较都很好

但是,如果您想比较忽略的情况,则最好使用“系统”。equalsIgnoreCase(env)


因为
env
在这一点上可能是空的

不是“等于”,所以在groovy中这是非常糟糕的实现,并且在字符串和GString之间返回false,尝试使用==您可以参考这个主题,因为我不确定为什么,但如果(“${env}”==“syst”)哦,是的,它工作了,我还有其他一些失败的地方,“==”非常有效。