0

I want to make function for typeconverter room. The original data is in List and i store it as String in database.

My code and the output is like this:

fun main() {
    val genreIds = listOf(28, 80)
    val stringFromList = fromList(genreIds)
    val stringToList = toList(stringFromList)
    
    println(genreIds) // output: [28, 80]
    println(stringFromList) // output: [28, 80]
    println(stringToList) // output: [91, 50, 56, 44, 32, 56, 48, 93]
}

fun fromList(genreIds: List<Int>): String = genreIds.toString()

fun toList(genreIdString: String): List<Int> {
    return genreIdString.map { it.toInt() }
}

I did try to use split(",") but it always give me error because of the "[" and "]". I want the output of toList function is [28, 80] too. Thanks for help.

3 Answers 3

2

You can use substring to remove the first and last character:

fun toList(genreIdString: String): List<Int> =
    genreIdString
        .substring(1, genreIdString.length - 1)
        .split(", ")
        .map { it.toInt() }
Sign up to request clarification or add additional context in comments.

Comments

1

It doesn't seem to be a good idea to parse the input manually. I propose to use Kotlin Serialization library for this purpose.

Using it you can write as simple as:

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

fun main() {
    val list = listOf(1, 2, 3)
    val str = Json.encodeToString(list)
    println(str) // "[1,2,3]"
    val list2: List<Int> = Json.decodeFromString(str)
    println(list2) // [1, 2, 3]
}

3 Comments

Thanks for the answer. Can you explain why it is better other than writing?
@wiryadev Using the toString() method you don't control the output - the result will depend on the method implementation which might change in the future. You can use toString() for debug purposes but if you need to store some data it's better to choose the approach which will give you predictable output.
I like Konstantin's argument of defensive programming against implementation changes. As an alternative solution, if you know that the strings that your toList method receives were created by the fromList method, then you could change your fromList method to genreIds.joinToString(", ", "[", "]"). Combine this with the solution by Aplet123 or by me, and you won't have to depend on the implementation anymore. Then again, I don't think the toString implementation is likely to change, so I would suggest choosing the solution that is easiest to read.
1

Use the removeSurrounding method to remove delimiters:

fun toList(genreIdString: String): List<Int> =
    genreIdString
        .removeSurrounding("[", "]")
        .split(", ")
        .map { it.toInt() }

I think this approach is better than using substring because it more clearly conveys your intent.

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.