https://kotlinlang.org logo
#android
Title
# android
v

Vivek Modi

06/24/2022, 9:06 AM
Hey guys, I have one string in which I want to split into list from there where I should provide character. I'll explain in details
For example 1
Copy code
val string = "Birth Control"
    
    val searchText = "n"
Output
Copy code
["Birth Co", "trol"]
For example 2
Copy code
val string = "Bladder Infection"

    val searchText = "i"
Actual Output
Copy code
["Bladder ", "nfect", "on"]
Expect Output
Copy code
["Bladder ", "nfection"]
I tried some code but
example 1
is working fine but
example 2
is not because I only want to split first occurrence.
Copy code
val splitList = title?.split(searchText, ignoreCase = true)?.toMutableList()
    splitList?.remove(searchText)
Can someone guide me how to solve this idiomatic way. Thanks
t

Thomas Urbanitsch

06/24/2022, 9:18 AM
there should also be a limit param on .split to specify the max number of substrings returned
Copy code
"Bladder Infection".split("i", ignoreCase = true, limit = 2)
->
[Bladder , nfection]
https://pl.kotl.in/poJnscjh-
v

Vivek Modi

06/24/2022, 9:38 AM
great thanks
3 Views