Kotlin .partition()
Published Nov 13, 2023Updated May 15, 2024
Contribute to Docs
.partition() takes a string parameter and returns two new strings. The first new string contains those characters that satisfy a supplied predicate, and the second new string contains those that do not satisfy that predicate.
A predicate takes an argument and returns a boolean true or false. If true is returned, the argument is said to satisfy the predicate. Otherwise, the argument does not satisfy the predicate.
Syntax
xString.partition(xPredicate)
xString: Supplies the string to be partitioned.xPredicate: Supplies the predicate that partitions the value ofxString.
Example
The following example uses the character method .isUpperCase() as the predicate within .partition() to partition a string into uppercase letter characters and other characters:
fun main () {val sampleString = "AbCd12/"println(sampleString.partition(){it.isUpperCase()})}
The above example results in the following output:
(AC, bd12/)
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours