1

I have a string array and I want to check if it contains some string while ignoring the case, so I do

businessCategories.contains(currentCategory, ignoreCase = true)

but it displays the error:

Cannot find a parameter with this name: ignoreCase

Why doesn't my list recognise the param ignoreCase? I read about the function in a few places: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/contains.html and https://bladecoder.medium.com/the-kotlin-standard-function-will-not-just-improve-readability-in-this-case-but-performance-as-well-5515822ce216, so why is ignoreCase not available?

5
  • businessCategories isn't a CharSequence, it's an array? The documentation you've linked is for a CharSequence. The function you're calling is this: kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/… Commented May 15, 2021 at 15:49
  • @HenryTwist just realized that lol. So list doesn't have ignoreCase method? Means I need to convert businessCategories to CharSequence first? Or is there a better way? Commented May 15, 2021 at 15:53
  • 1
    No it doesn't. It checks whether the list contains a specific element. Are you looking to check whether the list contains a specific string ignoring case? If so then you want something like this: elements.any { it.equals("Something", ignoreCase = true) } Commented May 15, 2021 at 15:55
  • 1
    You can't just use any Kotlin function on an array and expect it to check the whole array, that would be very ambiguous. Also you can't 'convert' a list to a CharSequence without defining what that means: concatenating it? Adding commas in between? Etc. Commented May 15, 2021 at 15:57
  • 1
    elements.any { it.equals("Something", ignoreCase = true) } is what I actually need. Can you post it as the answer so that I can accept it, and maybe add a little more explanation to it that may help new Kotlin learners understand more? Commented May 15, 2021 at 16:09

2 Answers 2

5

You seem to be mixing up your types here. The documentation you linked refers to the contains method on a CharSequence, which checks whether a string is a 'superstring' of another. What you're probably after is to check whether any element of your array is equal to a specific string, whilst ignoring case.

Unfortunately I don't think there is a function in the standard library to achieve this, but you can use Array<T>.any along with String?.equals to check each element, something like this:

val myString = "..."
stringArray.any { it.equals(myString, ignoreCase = true) }

You could even define a nice extension function:

fun Array<String>.contains(s: String, ignoreCase: Boolean = false): Boolean {
    
    return any { it.equals(s, ignoreCase) }
}
Sign up to request clarification or add additional context in comments.

Comments

0

There is no such feature in Kotlin itself, but in can be easily added using an extension function:

infix fun <T : CharSequence> T.inIgnoreCase(iterable: Iterable<T>): Boolean {
    return iterable.any { it.contentEquals(this, ignoreCase = true) }
}

It can be used this way:

val list = listOf("aaa", "bbb")
val string = "AAA"
string inIgnoreCase list // true

Comments

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.