Is there any function in std library to partition ...
# getting-started
s
Is there any function in std library to partition a string into Pair. I wanted to created pair from a property string. What i am doing right now is,
Copy code
val tuple = "key=value".split("=")
val pair =  when {
                  tuple.size > 1 -> tuple[0] to tuple[1]
                  else -> tuple[0] to ""
               }
d
suresh: What I could come up with:
Copy code
val tuple = "key=value".splitToSequence("=")
val pair = (tuple + generateSequence { "" }).take(2).toList()
Actually, that produces a list, not a pair. Hm.
s
How about this?
Copy code
val tuple = "key=value".split("=") + ""
val pair = tuple[0] to tuple[1]
d
that is way better 😄
s
Got the idea from your sample 😄
o
tuple[1]
will throw if there is no
=
in the string
missed
+""
sorry