- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin String - findAll() Function
The Kotlin string findAll() function belongs to the regex class. It is used to find all matches of a regular expression in a char sequence or string.
This function is not a direct function of the Kotlin string class. But it is invoked via a regex object.
Use Cases of FindAll()
There are the following use cases of this function −
- It finds repeated pattern of substring in a string.
- It can parse structured data, such as CSV fields, URLs, or phone numbers.
- It extract all occurrences of a pattern in a string.
Syntax
Following is the syntax of the Kotlin string findAll() function −
fun Regex.findAll(input: CharSequence, startIndex: Int = 0): MatchResult?
Parameters
This function accepts following parameters −
- CharSequence: The string or charSequence in which we search for a match.
- startIndex: It is an optional parameter that represents an index from which we start searching in a string. The default is 0.
Return value
This function returns a sequence match result.
Example 1: Extracting All Digit
Following is the basic example of findAll() function: extracting all digit from a string −
fun main() {
val regex = Regex("\\d+")
val string = "tut12oria234lspo67int"
val matches = regex.findAll(string)
println("Display all digits: ")
for (match in matches) {
print(match.value + " ")
}
}
Output
Following is the output −
Display all digits: 12 234 67
Example 2: Extracting All Words
Let's see another example of the findAll() function: extracting all words starting from a specific letter from a string −
fun main() {
// Matches words starting with 't'
val regex = Regex("\\bt\\w*")
val string = "tutorialspoint is a good edtech for tutorial. Keep Studying!"
val matches = regex.findAll(string)
for (match in matches) {
println(match.value)
}
}
Output
Following is the output −
tutorialspoint tutorial
Example 3: Pattern Details
This example uses the findAll() function and accesses the details of each pattern match −
fun main() {
// Matches one or more digits
val regex = Regex("\\d+")
val string = "Order 123: price is 456 and tax is 789."
val matches = regex.findAll(string)
for (match in matches) {
println("Matched value: ${match.value}")
println("Start index: ${match.range.first}")
println("End index: ${match.range.last}")
println("---")
}
}
Output
Following is the output −
Matched value: 123 Start index: 6 End index: 8 --- Matched value: 456 Start index: 20 End index: 22 --- Matched value: 789 Start index: 35 End index: 37 ---
kotlin_strings.htm
Advertisements