如何比较字符串?

如何比较作为参数传递的字符串

下面的方法不起作用。

 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"
}
}
313688 次浏览

This line:

if(str2==${str}){

Should be:

if( str2 == str ) {

The ${ and } will give you a parse error, as they should only be used inside Groovy Strings for templating

If you don't want to check on upper or lowercases you can use the following method.

String str = "India"
compareString(str)


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

So now if you change str to "iNdIa" it'll still work, so you lower the chance that you make a typo.

The shortest way (will print "not same" because String comparison is case sensitive):

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


compareString("India")

This should be an answer

str2.equals( str )

If you want to ignore case

str2.equalsIgnoreCase( str )

In Groovy, null == null gets a true. At runtime, you won't know what happened. In Java, == is comparing two references.

This is a cause of big confusion in basic programming, Whether it is safe to use equals. At runtime, a null.equals will give an exception. You've got a chance to know what went wrong.

Especially, you get two values from keys not exist in map(s), == makes them equal.

use def variable, when you want to compare any String. Use below code for that type of comparison.

def variable name = null

SQL query give you some return. Use function with return type def.

def functionname(def variablename){

return variable name

}

if ("$variable name" == "true"){

}

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
}

output:

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