Indent multi-line string

Here’s the code I made

fun main(args: Array<String>) {    
	val vars = """
    var one: String
    var two: String
    var three: String
    """.trimIndent()
    
    val generatedCode = """
class foo {
    $vars
}
 	""".trimIndent()
    
    println(generatedCode)
}

I want “generatedCode” string to look like this

class foo {
    var one: String
    var two: String
    var three: String
}

But it looks like this

class foo {
    var one: String
var two: String
var three: String
}

Only the first line is indented :frowning:

The problem is that your vars String does not have any indentation. That means that once you insert it the first line get’s the indentation from your generatedCode while the others end up without any ident.

If you are looking for a way to generate kotlin code I would take a look at something like Kotlin Poet.
I don’t really know another easy solution to your problem though. If you just need to generate a small amount of code you could maybe do the formatting yourself. I don’t think there is any easy utility function out there that can help you otherwise.

trimIndent removes the common indent.
remove it.

fun main(args: Array<String>) {    
	val vars = """var one: String
    var two: String
    var three: String"""
    
    val generatedCode = """
class foo {
    $vars
}
 	""".trimIndent()
    
    println(generatedCode)
}