How can you determine if two strings are equal in Groovy?

In Groovy, we can use the equals() method to determine if two strings are equal. The equals() method is a method of the String class, which compares the content of two strings to see if they are the same. Here is an example:

def str1 = "Hello"
def str2 = "hello"

if (str1.equals(str2)) {
    println("字符串相等")
} else {
    println("字符串不相等")
}

The output is “Strings are not equal” because the contents of str1 and str2 are not exactly the same. Please note that the equals() method is case-sensitive, if you want to ignore case during comparison, you can use the equalsIgnoreCase() method.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds