Groovy -如何比较字符串?

uyhoqukh  于 12个月前  发布在  其他
关注(0)|答案(7)|浏览(230)

如何比较作为参数传递的字符串
以下方法不起作用。

String str = "saveMe"

 compareString(str)

 def compareString(String str){
    def str2 = "saveMe"
    if(str2==${str}){
      println "same"
    }else{
      println "not same"
    }
 }

字符串
还试图

String str = "India"

 compareString(str)

 def compareString(String str){
   def str2 = "india"
   if( str2 == str ) {
     println "same"
   }else{
     println "not same"
   }
 }

2jcobegt

2jcobegt1#

这应该是一个答案:

str2.equals( str )

字符串
如果要忽略大小写:

str2.equalsIgnoreCase( str )

eblbsuwk

eblbsuwk2#

这一行:

if(str2==${str}){

字符串
应该是:

if( str2 == str ) {


${}将给您给予一个解析错误,因为它们只能在Groovy String中用于模板化

rsl1atfo

rsl1atfo3#

如果你不想检查大写或小写,你可以使用下面的方法。

String str = "India" 
compareString(str) 

def compareString(String str){ 
  def str2 = "india" 
  if( str2.toUpperCase() == str.toUpperCase() ) { 
    println "same" 
  }else{ 
    println "not same" 
  } 
}

字符串
所以现在如果你把str改为“iNdIa”,它仍然可以工作,所以你降低了打字错误的机会。

ql3eal8s

ql3eal8s4#

最短的方法(将打印“不相同”,因为字符串比较是区分大小写的):

def compareString = {
   it == "india" ? "same" : "not same"
}    

compareString("India")

字符串

taor4pac

taor4pac5#

String str = "saveMe"
compareString(str)

def compareString(String str){
  def str2 = "saveMe"

  // using single quotes
  println 'single quote string class' + 'String.class'.class
  println str + ' == ' + str2 + " ? " + (str == str2)
  println ' str = ' +  '$str' //  interpolation not supported

  // using double quotes, Interpolation supported
  println "double quoted string with interpolation " + "GString.class $str".class
  println "double quoted string without interpolation " + "String.class".class
  println "$str equals $str2 ? " + str.equals(str2) 
  println '$str == $str2 ? ' + "$str==$str2"
  println '${str == str2} ? ' + "${str==str2} ? "

  println '$str equalsIgnoreCase $str2 ? ' + str.equalsIgnoreCase(str2)   

  println '''
  triple single quoted Multi-line string, Interpolation not supported $str ${str2}
  Groovy has also an operator === that can be used for objects equality
  === is equivalent to o1.is(o2)
  '''
  println '''
  triple quoted string 
  '''
  println 'triple single quoted string ' + '''' string '''.class

  println """ 
  triple double quoted Multi-line string, Interpolation is supported $str == ${str2}
  just like double quoted strings with the addition that they are multiline
  '\${str == str2} ? ' ${str == str2} 
  """
  println 'triple double quoted string ' + """ string """.class
}

字符串
产出:

single quote string classclass java.lang.String
saveMe == saveMe ? true
str = $str
double quoted string with interpolation class org.codehaus.groovy.runtime.GStringImpl
double quoted string without interpolation class java.lang.String
saveMe equals saveMe ? true
$str == $str2 ? saveMe==saveMe
${str == str2} ? true ? 
$str equalsIgnoreCase $str2 ? true 

triple single quoted Multi-line string, Interpolation not supported $str ${str2}
Groovy has also an operator === that can be used for objects equality
=== is equivalent to o1.is(o2)

triple quoted string 

triple single quoted string class java.lang.String

triple double quoted Multi-line string, Interpolation is supported saveMe == saveMe
just like double quoted strings with the addition that they are multiline
'${str == str2} ? ' true 

triple double quoted string class java.lang.String

f87krz0w

f87krz0w6#

在Groovy中,null == null得到一个true。在运行时,你不会知道发生了什么。在Java中,==是比较两个引用。
这是基础编程中的一个大混乱的原因,使用equals是否安全。在运行时,null.equals将给予一个异常。你有机会知道哪里出错了。
特别是,你从map中不存在的键中得到两个值,==使它们相等。

iqjalb3h

iqjalb3h7#

当你想比较任何字符串时,使用def变量。使用下面的代码进行这种类型的比较。
def变量名= null
SQL查询给予一些返回值。使用返回类型为def的函数。
public int findDuplicate(int findDuplicate){
返回变量名
}
if(“$variable name”){
}

相关问题