70

Example:

String1 = "AbBaCca";
String2 = "bac";

I want to perform a check that String1 contains String2 or not.

7 Answers 7

109

Kotlin has stdlib package to perform certain extension function operation over the string, you can check this method it will check the substring in a string, you can ignore the case by passing true/false value. Refer this link

"AbBaCca".contains("bac", ignoreCase = true)
Sign up to request clarification or add additional context in comments.

2 Comments

and what about the functions find(), findAnyOf() or indexOf()? Is contains() the fastest method to do this?
At the time this question was asked we didn't have such utility methods, also performance depends on the input and size of the string you are using.
82

The most idiomatic way to check this is to use the in operator:

String2 in String1

This is equivalent to calling contains(), but shorter and more readable.

3 Comments

This is shorter, but in case you want to make negation, you will need brackets. At least it seems so. And If you have condition with two such checks, there stands to be over-bracketed. Thus sometimes CONTAINS() fit in some situations better than IN.
If you want to make negation, you can write this as String2 !in String1. You do not need parentheses.
This cannot be the correct one if you want to ignore case since it will call default contains.
17

You can do it by using the "in" - operator, e.g.

val url : String = "http://www.google.de"
val check : Boolean = "http" in url

check has the value true then. :)

1 Comment

It's the simpliest way
4

See the contains method in the documentation.

String1.contains(String2);

Comments

3

Kotlin has a few different contains function on Strings, see here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/contains.html.

If you want it to be true that string2 is contained in string1 (ie you want to ignore case), they even have a convenient boolean argument for you, so you won't need to convert to lowercase first.

Comments

1

For anyone out there like me who wanted to do this for a nullable String, that is, String?, here is my solution:

operator fun String?.contains(substring:String): Boolean {
    return if (this is String) {
        // Need to convert to CharSequence, otherwise keeps calling my
        // contains in an endless loop.
        val charSequence: CharSequence = this
        charSequence.contains(substring)
    } else {
        false
    }
}

// Uses Kotlin convention of converting 'in' to operator 'contains'
if (shortString in nullableLongString) {
    // TODO: Your stuff goes here!
}

Comments

0

in my case works codingjeremy answer, with littel changes:

operator fun String?.contains(substring: String?): Boolean {
 return if (this != null && substring != null) {
 val charSequence: CharSequence = this
   charSequence.contains(substring)
   } else {
   false
   }
 }

1 Comment

Code need formatting

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.