# Multiline string literal indent handling is unconvient

**URL:** <https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497>\
**Category:** Uncategorized\
**Created:** [August 31, 2014, 5:24pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497 "2014-08-31T17:24:28Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Unitcraft](https://avatars.discourse-cdn.com/v4/letter/u/2acd7d/32.png) [@Unitcraft](https://discuss.kotlinlang.org/u/Unitcraft)\
**Post date:** [August 31, 2014, 5:24pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/1 "2014-08-31T17:24:28Z")

</div>

I want to print:

```kotlin
line
line
```

I must write:

```kotlin
class TestMultiLine {
    Test fun test() {
        println("""line
line""")
    }
}
```

This way breaks code formatting.

Please, consider Fantom approach:

```kotlin
class TestMultiLine {
    Test fun test() {
        println("""line
                   line""")
    }
}
```

---

<div class="post-metadata">

**Author:** ![RobNik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RobNik](https://discuss.kotlinlang.org/u/RobNik)\
**Post date:** [August 31, 2014, 6:47pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/2 "2014-08-31T18:47:35Z")

</div>

Maybe something like this would be good enough for you?&nbsp;&nbsp;It's inspired by the same method `stripMargin` in Scala:

fun String.stripMargin(): String {  
 &nbsp;&nbsp;val lines = this.split(“n”)  
 &nbsp;&nbsp;return lines map { it.replaceAll(“^\s\*|”, “”) } join “n”  
}  
  
   
fun main(args: Array\<String\>) {  
 &nbsp;&nbsp;println(“”“Apples, oranges,  
 &nbsp;&nbsp;|bananas, kiwis,  
 &nbsp;&nbsp;|&nbsp;&nbsp;avocados, tomatoes”“”.stripMargin())  
  
   
}

  
You could shorten the name, or make it a val so no need for parentheses.

Rob

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [March 18, 2016, 9:13pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/3 "2016-03-18T21:13:22Z")

</div>

I’m not a fan of Fantom’s approach, but I would like to see something like Scala`s [stripMargin](http://alvinalexander.com/scala/scala-multiline-strings-heredoc-syntax#Stripping_the_spaces_off_of_Scala_multiline_strings) in the standard library.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [March 18, 2016, 9:25pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/4 "2016-03-18T21:25:59Z")

</div>

There are two convenient functions in stdlib for stripping margin from multiline strings: [`trimMargin`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-margin.html) and [`trimIndent`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-indent.html).

```auto
	fun test() {
        println("""|line
                   |line""".trimMargin())
        
        println("""
            line
            line
            """.trimIndent())        
    }

```

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 19, 2016, 2:36am UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/5 "2016-03-19T02:36:31Z")

</div>

> [@ilya.gorbunov](#):
>
> There are two convenient functions in stdlib for stripping margin from multiline strings: trimMargin and trimIndent.

The problem with that approach is the trimming is done at run time. It would be nice to have a way to tell the compiler to do it at compile time.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [March 30, 2016, 8:58pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/6 "2016-03-30T20:58:29Z")

</div>

> [@dalewking](#):
>
> The problem with that approach is the trimming is done at run time. It would be nice to have a way to tell the compiler to do it at compile time.

This seems like an ideal candidate for a compiler (not JVM) intrinsic. The nice thing about standard libraries is that functions from it can be replaced by intrinsics if so desired. Their API’s are kind of frozen by default anyway.

---

<div class="post-metadata">

**Author:** ![guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@guest](https://discuss.kotlinlang.org/u/guest)\
**Post date:** [June 22, 2016, 4:31pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/7 "2016-06-22T16:31:07Z")

</div>

I completely agree. A lot of time I write SQLs, HTML and other similar things in my code. Would be very nice to have such a functionality. And multiline string keeping extra left spaces is just crazy. This create a very strange code or a very strange string. I don’t know what would be the best rule the follow when removing this extra spaces, but I am sure it is possible to figure out some solution.

Leonardo Priori

---

<div class="post-metadata">

**Author:** ![lukas.eder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lukas.eder/32/2551_2.png) [@lukas.eder](https://discuss.kotlinlang.org/u/lukas.eder)\
**Post date:** [May 18, 2017, 12:04pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/8 "2017-05-18T12:04:31Z")

</div>

Xtend has solved this really nicely:

> **[Xtend - Expressions](https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html)**
>
> Xtend is a statically typed programming language sitting on top of Java.

Perhaps some inspiration there?

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [May 18, 2017, 2:52pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/9 "2017-05-18T14:52:39Z")

</div>

The “solution” is in the `String.trim...` functions that you can use to trim as desired. If these functions could now be defined as `constexpr` and evaluated at compile time your problem would go away. Alternatively, it may be possible to have a compiler plugin that recognizes these specific function calls and in case of a compile time constant parameter replaces it with the result of calling the function - constexpr is a lot cleaner as it is not limited to “special cases”.

---

<div class="post-metadata">

**Author:** ![BenLeggiero](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/benleggiero/32/2306_2.png) [@BenLeggiero](https://discuss.kotlinlang.org/u/BenLeggiero)\
**Post date:** [March 17, 2019, 4:41pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/10 "2019-03-17T16:41:07Z")

</div>

Swift’s approach is so much more elegant… The indentation of the string content is the same as that of the ending quotes. Always and without exception, checked and optimized at compile time. What more could you want?

---

<div class="post-metadata">

**Author:** ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)\
**Post date:** [April 5, 2019, 1:43pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/11 "2019-04-05T13:43:40Z")

</div>

Kotlin 1.3.40 will actually support intrinsics for these functions, see [https://youtrack.jetbrains.com/issue/KT-17755](https://youtrack.jetbrains.com/issue/KT-17755).

---

<div class="post-metadata">

**Author:** ![Aloso](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@Aloso](https://discuss.kotlinlang.org/u/Aloso)\
**Post date:** [August 6, 2019, 12:45pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/12 "2019-08-06T12:45:07Z")

</div>

Intriniscs only work for _constant_ strings, which is not that useful.

As soon as you write something like

```kotlin
x = """|Hello, $name!
       |How are you?""".trimMargin()

```

the trimming can’t be done at compile time, because Kotlin doesn’t know if `name` contains line breaks.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [August 6, 2019, 1:49pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/13 "2019-08-06T13:49:45Z")

</div>

Compile-time trimming is still useful for dynamic strings in my opinion. If the dynamic parts do not contain line breaks, it will work very well.

I agree that there should be an option to do the trimming at runtime. In many use cases, the developer will know whether to expect line breaks in the dynamic parts, and can choose accordingly.

---

<div class="post-metadata">

**Author:** ![Aloso](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@Aloso](https://discuss.kotlinlang.org/u/Aloso)\
**Post date:** [August 6, 2019, 2:05pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/14 "2019-08-06T14:05:05Z")

</div>

It’s useful, yes, but it can’t be done if it doesn’t work properly. Compile-time trimming would require a different syntax.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [August 6, 2019, 2:23pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/15 "2019-08-06T14:23:43Z")

</div>

Yes I see how the current implementation can break existing code.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [August 6, 2019, 2:30pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/16 "2019-08-06T14:30:35Z")

</div>

Correct me if I’m wrong but I don’t think `tirmMargin` or `trimIndent` are ever used with dynamic multiline strings. I don’t have real numbers for this but I’d imagine that in most cases a multiline string in a dynamic `trimMargin` is just a bug and not intended, so maybe changing this is not as bad of a breaking change than it seems.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [August 19, 2019, 12:12pm UTC](https://discuss.kotlinlang.org/t/multiline-string-literal-indent-handling-is-unconvient/497/17 "2019-08-19T12:12:33Z")

</div>

It may also be possible to translate the dynamic bits of the string into a call that is wrapped in its own trim function that can is given the needed information for correct wrapping by the compiler. It would probably be best implemented in an optimistic fashion (assuming that it doesn’t need to wrap).
