how to compare the string which is passed as a parameter
the following method is not working.
String str = "saveMe" compareString(str) def compareString(String str){ def str2 = "saveMe" if(str2==${str}){ println "same" }else{ println "not same" } }
also tried
String str = "India" compareString(str) def compareString(String str){ def str2 = "india" if( str2 == str ) { println "same" }else{ println "not same" } }
Groovy - compareTo() The compareTo method is to use compare one number against another. This is useful if you want to compare the value of numbers.
In Groovy == translates to a. compareTo(b)==0, if they are Comparable, and a. equals(b) otherwise.
In Groovy we use the == operator to see if two objects are the same, in Java we would use the equals() method for this. To test if two variables are referring to the same object instance in Groovy we use the is() method. The !=
This should be an answer
str2.equals( str )
If you want to ignore case
str2.equalsIgnoreCase( str )
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With