Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string by fixed number of chars [duplicate]

Is there a quick way to split a String by fixed number of chars in Kotlin?

I need to split this 544A1609B62E, lowercase it, add : where needed and achieve this: 54:4a:16:09:b6:2e.

What would be the idiomatic way to do it?

like image 718
lidkxx Avatar asked Dec 03 '25 18:12

lidkxx


1 Answers

In Kotlin 1.2 you can do this:

"544A1609B62E".toLowerCase().chunked(2).joinToString(":")

The chunked function is new in Kotlin 1.2. It splits a collection into chunks of the given size.

Doing this in Kotlin 1.1 or lower is a bit more cumbersome. You could look at the answers in the question posted by @NSimon here: Java: How to split a string by a number of characters?

like image 105
marstran Avatar answered Dec 06 '25 15:12

marstran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!