How to check whether a string contains a substring

2020-05-25 06:44发布

Example:

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

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

标签: string kotlin
4条回答
一夜七次
2楼-- · 2020-05-25 07:15

See the contains method in the documentation.

String1.contains(String2);
查看更多
你好瞎i
3楼-- · 2020-05-25 07:28

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.

查看更多
走好不送
4楼-- · 2020-05-25 07:31

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)
查看更多
Viruses.
5楼-- · 2020-05-25 07:39

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.

查看更多
登录 后发表回答