avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1529

Abstract

<p id="d2db">Learn about the <a href="http://kotlinlang.org/docs/reference/basic-types.html#string-literals">different string literals and string templates</a> in Kotlin.</p><p id="b7c8">You can use the handy library functions <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-indent.html"><code>trimInd</code>ent</a> and <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-margin.html"><code>trimMar</code>gin</a> to format multiline triple-quoted strings in accordance with the surrounding code.</p><p id="5e89">If you want, replace the <code>trimIndent</code> call bellow with a <code>trimMargin</code> call taking <code>#</code> as the prefix value, so that the resulting string doesn't contain the prefix character. Run the example to verify your solution.</p>
    <figure id="a263">
        <div>
          <div>
            <img class="ratio" src="http://placehold.it/16x9">
            <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2F0nhN3NM5C%3Ffrom%3D18%26to%3D24&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F0nhN3NM5C%3Ffrom%3D18%26to%3D24&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="7834">Triple-quoted strings are not only useful for multiline strings, but also for creating regex patterns, b

Options

ecause you don’t need to escape a backslash with a backslash.</p><figure id="e449"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*Cn2AR1F_pwvBt6Jq.jpg"><figcaption></figcaption></figure><p id="57dd">The following pattern matches a date in the format <code>13.06.1992</code> (two digits, a dot, two digits, a dot, four digits)</p><div id="75f6"><pre><span class="hljs-string">"""\d{2}.\d{2}.\d{4}"""</span></pre></div><p id="4331">Compare that with:</p><div id="5b88"><pre><span class="hljs-string">"<span class="hljs-subst">\</span>d{2}<span class="hljs-subst">\</span>.<span class="hljs-subst">\</span>d{2}<span class="hljs-subst">\</span>.<span class="hljs-subst">\</span>d{4}"</span></pre></div><p id="fb5a">God forbid you need to match an actual <code></code>, in which case you need 4 backslashes when using regular strings.</p><p id="95ce">Go back to the <a href="https://readmedium.com/ranges-288a61ec080e">Ranges</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/string-templates-92eb82841cbb">String Templates</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Triple-quoted Strings

Achieving better formatting and make it easier to work with regexes with triple-quoted strings, trimIndent and trimMargin

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Learn about the different string literals and string templates in Kotlin.

You can use the handy library functions trimIndent and trimMargin to format multiline triple-quoted strings in accordance with the surrounding code.

If you want, replace the trimIndent call bellow with a trimMargin call taking # as the prefix value, so that the resulting string doesn't contain the prefix character. Run the example to verify your solution.

Triple-quoted strings are not only useful for multiline strings, but also for creating regex patterns, because you don’t need to escape a backslash with a backslash.

The following pattern matches a date in the format 13.06.1992 (two digits, a dot, two digits, a dot, four digits)

"""\d{2}\.\d{2}\.\d{4}"""

Compare that with:

"\\d{2}\\.\\d{2}\\.\\d{4}"

God forbid you need to match an actual \, in which case you need 4 backslashes when using regular strings.

Go back to the Ranges, jump to the Table of Contents, or continue to String Templates.

Join me in Etnetera
Kotlin
Java
Programming
Formatting
Learning
Recommended from ReadMedium