String with triple quote inside of string with triple quote









up vote
1
down vote

favorite












I'm using string with triple quote inside of string with triple quote:



fun main(args: Array<String>) 
val firstStr = """

OLOLO
""".trimIndent()

val secondStr = """
$firstStr
new added text
""".trimIndent()

println("The firstStr is $firstStr")
println("The secondStr is $secondStr")



and I've got the following output:



The firstStr is 
OLOLO
The secondStr is
OLOLO
new added text


And I'm confused why the indent remains on the last line (" new added text")... Because I expected that trimIndent() method had to trim all indent (as documentation comment says this method




Detects a common minimal indent of all the input lines, removes it
from every line and also removes the first and the last lines if they
are blank (notice difference blank vs empty).




And at the same time I can cut this indent with trimMargin() with default parameter ("|"):



fun main(args: Array<String>) new added text
""".trimMargin()

println("The firstStr is $firstStr")
println("The secondStr is $secondStr")



In this case I've got the following output:



The firstStr is 
OLOLO
The secondStr is
OLOLO
new added text


But I can't understand why trimIndent() doesn't trim that indent...










share|improve this question

















  • 1




    Sounds like a bug.
    – Zoe
    Nov 9 at 16:18














up vote
1
down vote

favorite












I'm using string with triple quote inside of string with triple quote:



fun main(args: Array<String>) 
val firstStr = """

OLOLO
""".trimIndent()

val secondStr = """
$firstStr
new added text
""".trimIndent()

println("The firstStr is $firstStr")
println("The secondStr is $secondStr")



and I've got the following output:



The firstStr is 
OLOLO
The secondStr is
OLOLO
new added text


And I'm confused why the indent remains on the last line (" new added text")... Because I expected that trimIndent() method had to trim all indent (as documentation comment says this method




Detects a common minimal indent of all the input lines, removes it
from every line and also removes the first and the last lines if they
are blank (notice difference blank vs empty).




And at the same time I can cut this indent with trimMargin() with default parameter ("|"):



fun main(args: Array<String>) new added text
""".trimMargin()

println("The firstStr is $firstStr")
println("The secondStr is $secondStr")



In this case I've got the following output:



The firstStr is 
OLOLO
The secondStr is
OLOLO
new added text


But I can't understand why trimIndent() doesn't trim that indent...










share|improve this question

















  • 1




    Sounds like a bug.
    – Zoe
    Nov 9 at 16:18












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm using string with triple quote inside of string with triple quote:



fun main(args: Array<String>) 
val firstStr = """

OLOLO
""".trimIndent()

val secondStr = """
$firstStr
new added text
""".trimIndent()

println("The firstStr is $firstStr")
println("The secondStr is $secondStr")



and I've got the following output:



The firstStr is 
OLOLO
The secondStr is
OLOLO
new added text


And I'm confused why the indent remains on the last line (" new added text")... Because I expected that trimIndent() method had to trim all indent (as documentation comment says this method




Detects a common minimal indent of all the input lines, removes it
from every line and also removes the first and the last lines if they
are blank (notice difference blank vs empty).




And at the same time I can cut this indent with trimMargin() with default parameter ("|"):



fun main(args: Array<String>) new added text
""".trimMargin()

println("The firstStr is $firstStr")
println("The secondStr is $secondStr")



In this case I've got the following output:



The firstStr is 
OLOLO
The secondStr is
OLOLO
new added text


But I can't understand why trimIndent() doesn't trim that indent...










share|improve this question













I'm using string with triple quote inside of string with triple quote:



fun main(args: Array<String>) 
val firstStr = """

OLOLO
""".trimIndent()

val secondStr = """
$firstStr
new added text
""".trimIndent()

println("The firstStr is $firstStr")
println("The secondStr is $secondStr")



and I've got the following output:



The firstStr is 
OLOLO
The secondStr is
OLOLO
new added text


And I'm confused why the indent remains on the last line (" new added text")... Because I expected that trimIndent() method had to trim all indent (as documentation comment says this method




Detects a common minimal indent of all the input lines, removes it
from every line and also removes the first and the last lines if they
are blank (notice difference blank vs empty).




And at the same time I can cut this indent with trimMargin() with default parameter ("|"):



fun main(args: Array<String>) new added text
""".trimMargin()

println("The firstStr is $firstStr")
println("The secondStr is $secondStr")



In this case I've got the following output:



The firstStr is 
OLOLO
The secondStr is
OLOLO
new added text


But I can't understand why trimIndent() doesn't trim that indent...







string kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 16:14









Ksenia

654928




654928







  • 1




    Sounds like a bug.
    – Zoe
    Nov 9 at 16:18












  • 1




    Sounds like a bug.
    – Zoe
    Nov 9 at 16:18







1




1




Sounds like a bug.
– Zoe
Nov 9 at 16:18




Sounds like a bug.
– Zoe
Nov 9 at 16:18












2 Answers
2






active

oldest

votes

















up vote
2
down vote













The weirdness with trimIdent() appears to be caused by having an empty line to start with. It's also slightly picky with how consistent the leading tabs and spaces are throughout, although the leading empty line seems to throw everything out of whack no doubt. In light of that you could include the newline (n) within your println as a work-around.



fun main(args: Array<String>) 

val firstStr =
"""
OLOLO
""".trimIndent()

val secondStr =
"""
$firstStr
new added text
""".trimIndent()

println("The firstStr is n$firstStr")
println("The secondStr is n$secondStr")




Output:



The firstStr is
OLOLO
The secondStr is
OLOLO
new added text



String.trimIndent(): String Detects a common minimal indent of all
the input lines, removes it from every line and also removes the first
and the last lines if they are blank (notice difference blank vs
empty
). Note that blank lines do not affect the detected indent level. In case if there are non-blank lines with no leading whitespace characters (no indent at all) then the common indent is 0, and therefore this function doesn't change the indentation.



Doesn't preserve the original line endings.




The following statement: "notice difference blank vs empty", might hold the key to the riddle, although it's not exactly clear what they mean by that; definitely strange behavior (maybe a bug?).






share|improve this answer





























    up vote
    2
    down vote













    The issue isn't actually in trimIndent. It's related to how raw string literals work when they contain parameters. You start off with this:




    val firstStr = """

    OLOLO
    """.trimIndent()



    The result of that is that firstStr has this value:



    "
    OLOLO"


    This is then injected into secondStr with this code:




    val secondStr = """
    $firstStr
    new added text
    """.trimIndent()



    Now imagine that you actually typed this in instead of using a parameter value. You'd do it like this (and I'm going to replace a few spaces with underlines to clarify something):




    val secondStr = """

    ____OLOLO
    new added text
    """.trimIndent()



    That would all work fine. But when the value is injected into the raw string literal, that isn't actually what happens, as it's got no idea that you wanted to put spaces where I put underlines above. As far as the system is concerned you're telling it simply to put in a carriage return then OLOLO. So actually what happens is the value that's generated (before trimIndent is called) is this:




    val secondStr = """

    OLOLO
    new added text
    """.trimIndent()



    And then, by the time trimIndent starts working on it, first thing it does is try to look for a common indent, but it can't find one, as one of the lines (OLOLO) isn't indented. So there's nothing it can do. That's why the white space before new added text remains).



    You can see this if you put a breakpoint in the implementation of trimIndent to see what the receiver is (i.e. what value of secondStr is before trimIndent starts its work).



    So your solution is right. Add a pipe character and use trimMargin. The pipe means there aren't 4 leading spaces before the added text which is why it trims it all correctly.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229432%2fstring-with-triple-quote-inside-of-string-with-triple-quote%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      The weirdness with trimIdent() appears to be caused by having an empty line to start with. It's also slightly picky with how consistent the leading tabs and spaces are throughout, although the leading empty line seems to throw everything out of whack no doubt. In light of that you could include the newline (n) within your println as a work-around.



      fun main(args: Array<String>) 

      val firstStr =
      """
      OLOLO
      """.trimIndent()

      val secondStr =
      """
      $firstStr
      new added text
      """.trimIndent()

      println("The firstStr is n$firstStr")
      println("The secondStr is n$secondStr")




      Output:



      The firstStr is
      OLOLO
      The secondStr is
      OLOLO
      new added text



      String.trimIndent(): String Detects a common minimal indent of all
      the input lines, removes it from every line and also removes the first
      and the last lines if they are blank (notice difference blank vs
      empty
      ). Note that blank lines do not affect the detected indent level. In case if there are non-blank lines with no leading whitespace characters (no indent at all) then the common indent is 0, and therefore this function doesn't change the indentation.



      Doesn't preserve the original line endings.




      The following statement: "notice difference blank vs empty", might hold the key to the riddle, although it's not exactly clear what they mean by that; definitely strange behavior (maybe a bug?).






      share|improve this answer


























        up vote
        2
        down vote













        The weirdness with trimIdent() appears to be caused by having an empty line to start with. It's also slightly picky with how consistent the leading tabs and spaces are throughout, although the leading empty line seems to throw everything out of whack no doubt. In light of that you could include the newline (n) within your println as a work-around.



        fun main(args: Array<String>) 

        val firstStr =
        """
        OLOLO
        """.trimIndent()

        val secondStr =
        """
        $firstStr
        new added text
        """.trimIndent()

        println("The firstStr is n$firstStr")
        println("The secondStr is n$secondStr")




        Output:



        The firstStr is
        OLOLO
        The secondStr is
        OLOLO
        new added text



        String.trimIndent(): String Detects a common minimal indent of all
        the input lines, removes it from every line and also removes the first
        and the last lines if they are blank (notice difference blank vs
        empty
        ). Note that blank lines do not affect the detected indent level. In case if there are non-blank lines with no leading whitespace characters (no indent at all) then the common indent is 0, and therefore this function doesn't change the indentation.



        Doesn't preserve the original line endings.




        The following statement: "notice difference blank vs empty", might hold the key to the riddle, although it's not exactly clear what they mean by that; definitely strange behavior (maybe a bug?).






        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          The weirdness with trimIdent() appears to be caused by having an empty line to start with. It's also slightly picky with how consistent the leading tabs and spaces are throughout, although the leading empty line seems to throw everything out of whack no doubt. In light of that you could include the newline (n) within your println as a work-around.



          fun main(args: Array<String>) 

          val firstStr =
          """
          OLOLO
          """.trimIndent()

          val secondStr =
          """
          $firstStr
          new added text
          """.trimIndent()

          println("The firstStr is n$firstStr")
          println("The secondStr is n$secondStr")




          Output:



          The firstStr is
          OLOLO
          The secondStr is
          OLOLO
          new added text



          String.trimIndent(): String Detects a common minimal indent of all
          the input lines, removes it from every line and also removes the first
          and the last lines if they are blank (notice difference blank vs
          empty
          ). Note that blank lines do not affect the detected indent level. In case if there are non-blank lines with no leading whitespace characters (no indent at all) then the common indent is 0, and therefore this function doesn't change the indentation.



          Doesn't preserve the original line endings.




          The following statement: "notice difference blank vs empty", might hold the key to the riddle, although it's not exactly clear what they mean by that; definitely strange behavior (maybe a bug?).






          share|improve this answer














          The weirdness with trimIdent() appears to be caused by having an empty line to start with. It's also slightly picky with how consistent the leading tabs and spaces are throughout, although the leading empty line seems to throw everything out of whack no doubt. In light of that you could include the newline (n) within your println as a work-around.



          fun main(args: Array<String>) 

          val firstStr =
          """
          OLOLO
          """.trimIndent()

          val secondStr =
          """
          $firstStr
          new added text
          """.trimIndent()

          println("The firstStr is n$firstStr")
          println("The secondStr is n$secondStr")




          Output:



          The firstStr is
          OLOLO
          The secondStr is
          OLOLO
          new added text



          String.trimIndent(): String Detects a common minimal indent of all
          the input lines, removes it from every line and also removes the first
          and the last lines if they are blank (notice difference blank vs
          empty
          ). Note that blank lines do not affect the detected indent level. In case if there are non-blank lines with no leading whitespace characters (no indent at all) then the common indent is 0, and therefore this function doesn't change the indentation.



          Doesn't preserve the original line endings.




          The following statement: "notice difference blank vs empty", might hold the key to the riddle, although it's not exactly clear what they mean by that; definitely strange behavior (maybe a bug?).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 17:44

























          answered Nov 9 at 17:33









          l'L'l

          29.5k54891




          29.5k54891






















              up vote
              2
              down vote













              The issue isn't actually in trimIndent. It's related to how raw string literals work when they contain parameters. You start off with this:




              val firstStr = """

              OLOLO
              """.trimIndent()



              The result of that is that firstStr has this value:



              "
              OLOLO"


              This is then injected into secondStr with this code:




              val secondStr = """
              $firstStr
              new added text
              """.trimIndent()



              Now imagine that you actually typed this in instead of using a parameter value. You'd do it like this (and I'm going to replace a few spaces with underlines to clarify something):




              val secondStr = """

              ____OLOLO
              new added text
              """.trimIndent()



              That would all work fine. But when the value is injected into the raw string literal, that isn't actually what happens, as it's got no idea that you wanted to put spaces where I put underlines above. As far as the system is concerned you're telling it simply to put in a carriage return then OLOLO. So actually what happens is the value that's generated (before trimIndent is called) is this:




              val secondStr = """

              OLOLO
              new added text
              """.trimIndent()



              And then, by the time trimIndent starts working on it, first thing it does is try to look for a common indent, but it can't find one, as one of the lines (OLOLO) isn't indented. So there's nothing it can do. That's why the white space before new added text remains).



              You can see this if you put a breakpoint in the implementation of trimIndent to see what the receiver is (i.e. what value of secondStr is before trimIndent starts its work).



              So your solution is right. Add a pipe character and use trimMargin. The pipe means there aren't 4 leading spaces before the added text which is why it trims it all correctly.






              share|improve this answer


























                up vote
                2
                down vote













                The issue isn't actually in trimIndent. It's related to how raw string literals work when they contain parameters. You start off with this:




                val firstStr = """

                OLOLO
                """.trimIndent()



                The result of that is that firstStr has this value:



                "
                OLOLO"


                This is then injected into secondStr with this code:




                val secondStr = """
                $firstStr
                new added text
                """.trimIndent()



                Now imagine that you actually typed this in instead of using a parameter value. You'd do it like this (and I'm going to replace a few spaces with underlines to clarify something):




                val secondStr = """

                ____OLOLO
                new added text
                """.trimIndent()



                That would all work fine. But when the value is injected into the raw string literal, that isn't actually what happens, as it's got no idea that you wanted to put spaces where I put underlines above. As far as the system is concerned you're telling it simply to put in a carriage return then OLOLO. So actually what happens is the value that's generated (before trimIndent is called) is this:




                val secondStr = """

                OLOLO
                new added text
                """.trimIndent()



                And then, by the time trimIndent starts working on it, first thing it does is try to look for a common indent, but it can't find one, as one of the lines (OLOLO) isn't indented. So there's nothing it can do. That's why the white space before new added text remains).



                You can see this if you put a breakpoint in the implementation of trimIndent to see what the receiver is (i.e. what value of secondStr is before trimIndent starts its work).



                So your solution is right. Add a pipe character and use trimMargin. The pipe means there aren't 4 leading spaces before the added text which is why it trims it all correctly.






                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  The issue isn't actually in trimIndent. It's related to how raw string literals work when they contain parameters. You start off with this:




                  val firstStr = """

                  OLOLO
                  """.trimIndent()



                  The result of that is that firstStr has this value:



                  "
                  OLOLO"


                  This is then injected into secondStr with this code:




                  val secondStr = """
                  $firstStr
                  new added text
                  """.trimIndent()



                  Now imagine that you actually typed this in instead of using a parameter value. You'd do it like this (and I'm going to replace a few spaces with underlines to clarify something):




                  val secondStr = """

                  ____OLOLO
                  new added text
                  """.trimIndent()



                  That would all work fine. But when the value is injected into the raw string literal, that isn't actually what happens, as it's got no idea that you wanted to put spaces where I put underlines above. As far as the system is concerned you're telling it simply to put in a carriage return then OLOLO. So actually what happens is the value that's generated (before trimIndent is called) is this:




                  val secondStr = """

                  OLOLO
                  new added text
                  """.trimIndent()



                  And then, by the time trimIndent starts working on it, first thing it does is try to look for a common indent, but it can't find one, as one of the lines (OLOLO) isn't indented. So there's nothing it can do. That's why the white space before new added text remains).



                  You can see this if you put a breakpoint in the implementation of trimIndent to see what the receiver is (i.e. what value of secondStr is before trimIndent starts its work).



                  So your solution is right. Add a pipe character and use trimMargin. The pipe means there aren't 4 leading spaces before the added text which is why it trims it all correctly.






                  share|improve this answer














                  The issue isn't actually in trimIndent. It's related to how raw string literals work when they contain parameters. You start off with this:




                  val firstStr = """

                  OLOLO
                  """.trimIndent()



                  The result of that is that firstStr has this value:



                  "
                  OLOLO"


                  This is then injected into secondStr with this code:




                  val secondStr = """
                  $firstStr
                  new added text
                  """.trimIndent()



                  Now imagine that you actually typed this in instead of using a parameter value. You'd do it like this (and I'm going to replace a few spaces with underlines to clarify something):




                  val secondStr = """

                  ____OLOLO
                  new added text
                  """.trimIndent()



                  That would all work fine. But when the value is injected into the raw string literal, that isn't actually what happens, as it's got no idea that you wanted to put spaces where I put underlines above. As far as the system is concerned you're telling it simply to put in a carriage return then OLOLO. So actually what happens is the value that's generated (before trimIndent is called) is this:




                  val secondStr = """

                  OLOLO
                  new added text
                  """.trimIndent()



                  And then, by the time trimIndent starts working on it, first thing it does is try to look for a common indent, but it can't find one, as one of the lines (OLOLO) isn't indented. So there's nothing it can do. That's why the white space before new added text remains).



                  You can see this if you put a breakpoint in the implementation of trimIndent to see what the receiver is (i.e. what value of secondStr is before trimIndent starts its work).



                  So your solution is right. Add a pipe character and use trimMargin. The pipe means there aren't 4 leading spaces before the added text which is why it trims it all correctly.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 9 at 21:46

























                  answered Nov 9 at 16:36









                  Yoni Gibbs

                  928112




                  928112



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229432%2fstring-with-triple-quote-inside-of-string-with-triple-quote%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                      Edmonton

                      Crossroads (UK TV series)