Home > Software design >  Compare Strings Kotlin
Compare Strings Kotlin

Time:10-28

I have two lines of the same length. I need to get the number of letters that match as letters and have the same index in the string.

I wrote a method with a for loop, but there is a method without it.

Could you tell me how I can do this?

for (i in str1.indices) {
        if (str1[i] == str2[i]) {
            count  
        }
    }

CodePudding user response:

Using zip, you could write

(str1 zip str2).count { (a, b) -> a == b }
  • Related