- How to check if String contains Substring in Kotlin?
- Kotlin – Check if string contains specific search string
- Examples
- 1. String contains specified search string
- 2. Specified search string is not present in given string
- Conclusion
- Related Tutorials
- indexOf
- Parameters
- Parameters
- How to find Index of Substring in String in Kotlin?
- Kotlin – Index of Substring in String
- Syntax
- Examples
- 1. Find index of substring “apple”
- 2. Find index of substring with search from a specific start
- 3. Find index of substring with search ignoring case
- 4. Find index of substring – Negative Scenario
- Conclusion
- Related Tutorials
How to check if String contains Substring in Kotlin?
In this tutorial, you shall learn how to check if a given string contains specific search string in Kotlin, using String.contains() function, with examples.
Kotlin – Check if string contains specific search string
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.
contains() method returns true if the string str2 is present in the string str1 , else it returns false.
Examples
1. String contains specified search string
In this example, we will take two strings in str1 and str2 , and check if the string str1 contains the string str2 using String.contains() method.
Kotlin Program
The result of str1.contains(str2) is: true String str1 contains the string str2.
2. Specified search string is not present in given string
In this example, we will take two strings in str1 and str2 such that str2 is not present in str1 . str1.contains(str2) should return false.
Kotlin Program
The result of str1.contains(str2) is: false String str1 does not contain the string str2.
Conclusion
In this Kotlin Tutorial, we learned how to check if given string contains a specified string value in it, using String.contains() method, with the help of Kotlin example programs.
Related Tutorials
- How to Check if String Ends with Specified Character in Kotlin?
- How to Check if String Ends with Specified String in Kotlin?
- How to Check if String Starts with Specified Character in Kotlin?
- How to Check if String Starts with Specified String Value in Kotlin?
- How to Check if Two Strings are Equal in Kotlin?
- How to Compare Strings in Kotlin?
- How to Compare Strings in Kotlin?
- How to Create an Empty String in Kotlin?
- How to Filter Characters of String in Kotlin?
- How to Filter List of Strings based on Length in Kotlin?
- How to Filter only Non-Empty Strings of Kotlin List?
- How to Filter only Strings from a Kotlin List?
- How to Get Character at Specific Index of String in Kotlin?
- How to Initialize String in Kotlin?
- How to Iterate over Each Character in the String in Kotlin?
- How to Remove First N Characters from String in Kotlin?
- How to Remove Last N Characters from String in Kotlin?
- How to create a Set of Strings in Kotlin?
- How to define a List of Strings in Kotlin?
- How to define a String Constant in Kotlin?
- How to get Substring of a String in Kotlin?
- Kotlin String Concatenation
- Kotlin String Length
- Kotlin String Operations
- Kotlin String.capitalize() – Capitalize First Character
- Kotlin – Split String to Lines – String.lines() function
- Kotlin – Split String – Examples
- Kotlin – String Replace – Examples
- Kotlin – String to Integer – String.toInt()
- [Solved] Kotlin Error: Null can not be a value of a non-null type String
indexOf
Returns the index within this string of the first occurrence of the specified character, starting from the specified startIndex.
Parameters
ignoreCase — true to ignore character case when matching a character. By default false .
Return An index of the first occurrence of char or -1 if none is found.
fun CharSequence . indexOf (
string : String ,
startIndex : Int = 0 ,
ignoreCase : Boolean = false
) : Int
(source)
Returns the index within this char sequence of the first occurrence of the specified string, starting from the specified startIndex.
import java.util.Locale import kotlin.test.* fun main(args: Array) < //sampleStart fun matchDetails(inputString: String, whatToFind: String, startIndex: Int = 0): String < val matchIndex = inputString.indexOf(whatToFind, startIndex) return "Searching for '$whatToFind' in '$inputString' starting at position $startIndex: " + if (matchIndex >= 0) "Found at $matchIndex" else "Not found" > val inputString = "Never ever give up" val toFind = "ever" println(matchDetails(inputString, toFind)) // Searching for 'ever' in 'Never ever give up' starting at position 0: Found at 1 println(matchDetails(inputString, toFind, 2)) // Searching for 'ever' in 'Never ever give up' starting at position 2: Found at 6 println(matchDetails(inputString, toFind, 10)) // Searching for 'ever' in 'Never ever give up' starting at position 10: Not found //sampleEnd >
Parameters
ignoreCase — true to ignore character case when matching a string. By default false .
Return An index of the first occurrence of string or -1 if none is found.
How to find Index of Substring in String in Kotlin?
In this tutorial, you shall learn how to get the index of a specific search string in a given string in Kotlin, using String.indexOf() function, with examples.
Kotlin – Index of Substring in String
To find index of substring in this string in Kotlin, call indexOf() method on this string, and pass the substring as argument.
String.indexOf() returns an integer representing the index of first occurrence of the match for the given substring. If there is no match found for the given substring, then indexOf() returns -1 .
Syntax
The syntax of indexOf() method is
fun CharSequence.indexOf( string: String, startIndex: Int = 0, ignoreCase: Boolean = false ): Int
Examples
1. Find index of substring “apple”
In the following example, we take a string in str and find the index of the substring «apple» in str string.
2. Find index of substring with search from a specific start
We can also provide an optional start index for second argument. The search for the substring in this string happens from this start index.
In the following example, we find the index of the substring «apple» in the string str from the start index 8 .
Since we are searching from start index 8 , the first occurrence of the given substring is ignored.
3. Find index of substring with search ignoring case
We can also try to find the match by ignoring the case.
In the following example, we find the index of the substring «APPLE» in the string str .
4. Find index of substring – Negative Scenario
Now, let us take a substring which is not present in the string, and find the index returned by indexOf() method.
Conclusion
In this Kotlin Tutorial, we learned how to find the index of a substring in the given string using String.indexOf(), with examples.
Related Tutorials
- How to Check if String Ends with Specified Character in Kotlin?
- How to Check if String Ends with Specified String in Kotlin?
- How to Check if String Starts with Specified Character in Kotlin?
- How to Check if String Starts with Specified String Value in Kotlin?
- How to Check if Two Strings are Equal in Kotlin?
- How to Compare Strings in Kotlin?
- How to Compare Strings in Kotlin?
- How to Create an Empty String in Kotlin?
- How to Filter Characters of String in Kotlin?
- How to Filter List of Strings based on Length in Kotlin?
- How to Filter only Non-Empty Strings of Kotlin List?
- How to Filter only Strings from a Kotlin List?
- How to Get Character at Specific Index of String in Kotlin?
- How to Initialize String in Kotlin?
- How to Iterate over Each Character in the String in Kotlin?
- How to Remove First N Characters from String in Kotlin?
- How to Remove Last N Characters from String in Kotlin?
- How to check if a String contains Specified String in Kotlin?
- How to create a Set of Strings in Kotlin?
- How to define a List of Strings in Kotlin?
- How to define a String Constant in Kotlin?
- How to get Substring of a String in Kotlin?
- Kotlin String Concatenation
- Kotlin String Length
- Kotlin String Operations
- Kotlin String.capitalize() – Capitalize First Character
- Kotlin – Split String to Lines – String.lines() function
- Kotlin – Split String – Examples
- Kotlin – String Replace – Examples
- Kotlin – String to Integer – String.toInt()
- [Solved] Kotlin Error: Null can not be a value of a non-null type String