0

I have a string input of the following format:

[[10.5,125.7], [60.5, 25.6] ....]]

How can one parse something like this in Kotlin? This should end up as array of arrays of floats.

1
  • 1
    Assuming this is JSON, I suggest using some library for parsing JSON format. Even if this is for a very small thing. We should avoid writing our own data parsers. Commented Feb 14, 2022 at 19:20

1 Answer 1

2

For a List<List<Float>>:

val result = text
  .removeSurrounding("[[", "]]").split("], [", "],[")
  .map { it.split(",").map { s -> s.toFloat() } }

For an Array<Array<Float>>:

val result = text
  .removeSurrounding("[[", "]]").split("], [", "],[")
  .map { it.split(",").map { s -> s.toFloat() }.toTypedArray() }
  .toTypedArray()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.