0

I currently have the following code

fun main() {
    // Build.Fingerprint sample strings
    val debug_fingerprint: String? = "Company/device/device:11/3526.4353/0064504902000:userdebug/com-d,dev-keys"
    val dev_fingerprint: String? = "Company/device/device:11/526.4353/0064504902000:user/com-d,dev-keys"
    val user_fingerprint: String? = "Company/device/device:11/526.4353/0064504902000:user/com-p,release-keys"
    

    //Testing
    val osVarient: String = user_fingerprint?.let {
            when {
                check(listOf("userdebug", "dev-keys"), it) -> "Userdebug"
                check(listOf("user", "dev-keys"), it) -> "Userdevsigned"
                check(listOf("user", "release-keys"), it) -> "User"
                else -> "Unknown variant"
            }
        } ?: run {
            "Unknown variant null"
        }  
    print(osVarient)
}
fun check(args: List<String>,fingerprint: String): Boolean {
    for(arg in args) {
        if(!fingerprint.contains(arg)){
            return false
        }
    }
    return true
}

The above code works but I'm wondering if there is more elegant way of writing the code.

Are there any alternatives in Kotlin to compare multiple substrings to a string?

2
  • Do you only want to return True/False based on whether one or more of the substrings is present in the string? Commented Sep 19, 2022 at 18:38
  • I want to return true only if all substrings are present in the string. Else, return false Commented Sep 19, 2022 at 18:48

1 Answer 1

2

You may use a dictionary for OS variants. Iterable<T>.all is a builtin kotlin stdlib function to check a predicate on all elements.

fun main() {
  val userFingerprint: String? = "Company/device/device:11/526.4353/0064504902000:user/com-p,release-keys"

  val variantMap = mapOf(
      "Userdebug" to listOf("userdebug", "dev-keys"),
      "Userdevsigned" to listOf("user", "dev-keys"),
      "User" to listOf("user", "release-keys")
  )

  val osVariant = userFingerprint?.let { fingerprint ->
    variantMap.entries
        .firstOrNull { check(it.value, fingerprint) }
        ?.key
  } ?: "Unknown variant null"

  println(osVariant)
}

fun check(fingerprintTypes: List<String>, fingerprint: String): Boolean {
  return fingerprintTypes.all { it in fingerprint } 
  // return fingerprintTypes.all { fingerprint.contains(it) }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Or, slightly more concisely: fingerprintTypes.all{ it in fingerprint }.
Kotlin has always a way to make code more concise. Thanks @gidds
My pleasure :-) (I dunno why, but that use of in seems to be a common blind-spot — I rarely see anyone else using it, even though I find it easier to read than calling contains()…)
I use also in but in this question it did not pop up. @gidds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.