Hey I am working in kotlin. 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
val string = "Birth Control" val searchText = "n"
["Birth Co", "trol"]
For example 2
val string = "Bladder Infection" val searchText = "i"
["Bladder ", "nfect", "on"]
["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.
val splitList = title?.split(searchText, ignoreCase = true)?.toMutableList() splitList?.remove(searchText)
Can someone guide me how to solve this idiomatic way. Thanks
You miss the limit option of the split function. If you give it a value of 2 the result list will have a maximum of 2 entries:
val result = "Bladder Infection".split("i", ignoreCase = true, limit = 2)