Created
August 21, 2021 21:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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