Skip to content

Instantly share code, notes, and snippets.

@truedem
Created August 21, 2021 21:16
Show Gist options
  • Save truedem/47aebfdcb8227b7b2d6180e264497f87 to your computer and use it in GitHub Desktop.
Save truedem/47aebfdcb8227b7b2d6180e264497f87 to your computer and use it in GitHub Desktop.
Kotlin extentions to check out if all words are presented in the string
// Usage:
// if (fullname.containsWords(searchString)) { doStuff() }
fun String.containsWords(str: String?): Boolean {
val words = str?.trim()?.lowercase()?.split("\\s".toRegex())?.toTypedArray()
return Arrays.stream(words).allMatch { s: CharSequence? -> this.lowercase().contains(s.toString().lowercase()!!) }
}
// Usage:
// val words = searchString.trim().split("\\s".toRegex()).toTypedArray()
// if (fullname.lowercase().containsWords(words)) { doStuff() }
fun String.containsWords(words: Array<String>?): Boolean {
return Arrays.stream(words).allMatch { s: CharSequence? -> this.lowercase().contains(s.toString().lowercase()!!) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment