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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2F0nhN3NM5C%3Ffrom%3D18%26to%3D24&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&utm_medium=GabrielShanahan&utm_campaign=KotlinPrimer&utm_content=join-our-team&utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>