How to insert delimiter in a string with kotlin

Viewed 160

I have a mac string:

mac=7A2918D5434F

And I need to convert to this:

mac=7A:29:18:D5:43:4F

How can I do that in kotlin?

1 Answers

If you are sure that your initial string is correct you can do something like:

"7A2918D5434F".chunked(2).joinToString(":")

chunked(2) splits the string in chunks of size 2 (can be used for any Iterable).
jointToString(":") takes a list, joins the elements to string using : as delimiter

Related