The Java String contains function is a method that is used to check whether a particular sequence of characters is present in a given string or not. It returns a boolean value of true if the specified sequence of characters is found in the string, and false otherwise. The method takes a single argument, which is the sequence of characters to be searched for in the string. It is case-sensitive, meaning that it will only match the exact sequence of characters provided. The contains function is commonly used in string manipulation and searching operations in Java programming. Keep reading below to learn how to Java String contains in Kotlin.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Java String contains in Kotlin With Example Code

Java String contains() method is used to check whether a particular sequence of characters is present in a given string or not. In Kotlin, we can use the same method to check if a string contains a particular substring.

To use the contains() method in Kotlin, we simply need to call it on the string we want to check and pass the substring we want to check for as an argument. The method returns a boolean value indicating whether the substring is present in the string or not.

Here’s an example code snippet that demonstrates the usage of the contains() method in Kotlin:


val str = "Hello, World!"
val substr = "World"

if (str.contains(substr)) {
println("Substring found!")
} else {
println("Substring not found.")
}

In the above code, we first define a string variable str and a substring variable substr. We then call the contains() method on the str variable and pass the substr variable as an argument. If the method returns true, we print “Substring found!” to the console. Otherwise, we print “Substring not found.”.

It’s important to note that the contains() method is case-sensitive. So, if you want to perform a case-insensitive search, you can convert both the string and the substring to lowercase or uppercase before calling the method.

In conclusion, the contains() method in Kotlin is a simple and effective way to check if a string contains a particular substring.

Equivalent of Java String contains in Kotlin

In conclusion, the Kotlin programming language provides a more concise and efficient way of checking if a string contains a specific substring. The equivalent Java String contains function in Kotlin is the “contains” extension function, which can be called directly on a string variable. This function returns a boolean value indicating whether the substring is present in the string or not. By using this function, Kotlin developers can write cleaner and more readable code, while also taking advantage of the language’s modern features and capabilities. Overall, the “contains” function is a valuable tool for any Kotlin developer who needs to check for the presence of a substring in a string.

Contact Us