In Kotlin, I'm trying to create a filename dynamically that includes a type and a date like this:
var filename = "ab_$type_$date.dat"
However, the second underscore in between the variables is causing a compile error:
Kotlin: Unresolved reference: name_
I know I could concatenate the strings in the old fashion way:
var filename = "ab_" + type + "_$date.dat"
But I'm wondering if there is a different way to accomplish the same thing. Is there a way to escape special characters in string templates or any other way to get this to work?
String interpolation in Kotlin allows us to easily concatenate constant strings and variables to build another string elegantly.
The standard solution to split a string in Kotlin is with the native split() function, which takes one or more delimiters as an argument and splits the string around occurrences of the specified delimiters. The split() function returns a list of strings.
Just wrap your expressions in curly braces:
var filename = "ab_${type}_${date}.dat"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With