Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a string contains a substring in Kotlin?

Tags:

string

kotlin

Example:

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

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

like image 895
Anisuzzaman Babla Avatar asked Jul 09 '18 05:07

Anisuzzaman Babla


People also ask

How do I check if a string contains a substring in Kotlin?

To check if a string contains specified string in Kotlin, use String. contains() method. Given a string str1 , and if we would like to check if the string str2 is present in the string str1 , call contains() method on string str1 and pass the the string str2 as argument to the method as shown below.

How do you check if a string contains a specific substring?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

What does ?: Mean in Kotlin?

In certain computer programming languages, the Elvis operator ?: is a binary operator that returns its first operand if that operand is true , and otherwise evaluates and returns its second operand.

How do I check if a string contains a specific character?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.


1 Answers

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) 
like image 67
Aseem Sharma Avatar answered Sep 23 '22 16:09

Aseem Sharma