Question Locked

"Split" error when running code for application

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I wrote a code in Kotlin and when I run it I get an error Unresolved reference: split. I do not understand something how to fix this, because so far there is little practice with this programming language.

fun main() {
val str = "A:B:C"
val delim = ":"

val list = str.split(delim)

println(list) // [A, B, C]
}

2 replies

by kees_b edited

Apparently, it doesn't know the method split for the variable str. Better study the documentation of the language.

by Rohit Sri

The code you provided seems correct, and the error you're encountering is likely due to a typographical issue. In Kotlin, strings are enclosed in double quotes ("), but in your code, you used typographic quotes (“”).

To fix the error, replace the typographic quotes around the str variable and the delim variable with regular double quotes. Here's the corrected code

fun main() {
val str = "A:B:C"
val delim = ":"

val list = str.split(delim)

println(list) // [A, B, C]
}

After making this change, the split function should work as expected, splitting the string str at each occurrence of the delimiter delim.

This discussion is closed to new replies.