Raw String Literals - Remove Leading Indentation












7















Edit: Raw String Literals have been dropped from JDK 12, but I'm going to leave this question open and will edit it accordingly whenever Raw String Literals are reintroduced.





When testing Raw String Literals (which are a preview feature in Java 12), I came across the following snippet of code:



System.out.println(`
Test 1
Test 2
Test 3
`);


Which outputs the following:



          
Test 1
Test 2
Test 3
 


However, I want the output to resemble the following:



Test 1
Test 2
Test 3


What is the easiest way to remove the leading indentation to match the intended format?










share|improve this question





























    7















    Edit: Raw String Literals have been dropped from JDK 12, but I'm going to leave this question open and will edit it accordingly whenever Raw String Literals are reintroduced.





    When testing Raw String Literals (which are a preview feature in Java 12), I came across the following snippet of code:



    System.out.println(`
    Test 1
    Test 2
    Test 3
    `);


    Which outputs the following:



              
    Test 1
    Test 2
    Test 3
     


    However, I want the output to resemble the following:



    Test 1
    Test 2
    Test 3


    What is the easiest way to remove the leading indentation to match the intended format?










    share|improve this question



























      7












      7








      7


      1






      Edit: Raw String Literals have been dropped from JDK 12, but I'm going to leave this question open and will edit it accordingly whenever Raw String Literals are reintroduced.





      When testing Raw String Literals (which are a preview feature in Java 12), I came across the following snippet of code:



      System.out.println(`
      Test 1
      Test 2
      Test 3
      `);


      Which outputs the following:



                
      Test 1
      Test 2
      Test 3
       


      However, I want the output to resemble the following:



      Test 1
      Test 2
      Test 3


      What is the easiest way to remove the leading indentation to match the intended format?










      share|improve this question
















      Edit: Raw String Literals have been dropped from JDK 12, but I'm going to leave this question open and will edit it accordingly whenever Raw String Literals are reintroduced.





      When testing Raw String Literals (which are a preview feature in Java 12), I came across the following snippet of code:



      System.out.println(`
      Test 1
      Test 2
      Test 3
      `);


      Which outputs the following:



                
      Test 1
      Test 2
      Test 3
       


      However, I want the output to resemble the following:



      Test 1
      Test 2
      Test 3


      What is the easiest way to remove the leading indentation to match the intended format?







      java string indentation rawstring java-12






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 19 '18 at 23:55







      Jacob G.

















      asked Nov 18 '18 at 2:51









      Jacob G.Jacob G.

      15.3k52262




      15.3k52262
























          1 Answer
          1






          active

          oldest

          votes


















          8














          Accompanying Raw String Literals as a preview feature in Java 12 are new methods that will be added to java.lang.String, one of which is String#align. Its documentation states the following:




          Removes vertical and horizontal white space margins from around the
          essential body of a multi-line string, while preserving relative
          indentation.



          ...



          For each non-blank line, min leading white space characters are
          removed. Each white space character is treated as a single character. In
          particular, the tab character "t" (U+0009) is considered a
          single character; it is not expanded.



          Leading and trailing blank lines, if any, are removed. Trailing spaces are
          preserved.



          Each line is suffixed with a line feed character "n" (U+000A).




          To use this method, we can change the code to the following:



          System.out.println(`
          Test 1
          Test 2
          Test 3
          `.align());


          Which outputs the following (suffixed with a line feed character, as stated by the documentation):



          Test 1
          Test 2
          Test 3





          share|improve this answer



















          • 2





            To add to it :- this can also be attained using align(0) where the argument takes in input the indentation whitespaces equivalent then to align().indent(0)

            – nullpointer
            Nov 18 '18 at 7:48






          • 1





            Bad that the String API Javadoc doesn't list it still.

            – nullpointer
            Nov 18 '18 at 7:50






          • 3





            So you embed a rather large constant string into your code, just to perform an expensive operation on it after instantiation, to be repeated every time, this statement is executed. Compared to the good old "Test 1n" + " Test 2n" + " Test 3n" approach, this looks like a step backwards…

            – Holger
            Nov 19 '18 at 9:49











          • @Holger Though not in much favor of the raw strings, yet the indentation in the example from the question is missing from that of yours. That fanciness if I could term it, is what the new feature is adding to. Thoughts?

            – nullpointer
            Nov 19 '18 at 16:56








          • 1





            @Holger "this looks like a step backwards" - this will be solved by openjdk.java.net/jeps/303

            – ZhekaKozlov
            Dec 4 '18 at 14:02













          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',
          autoActivateHeartbeat: false,
          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%2f53357469%2fraw-string-literals-remove-leading-indentation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          8














          Accompanying Raw String Literals as a preview feature in Java 12 are new methods that will be added to java.lang.String, one of which is String#align. Its documentation states the following:




          Removes vertical and horizontal white space margins from around the
          essential body of a multi-line string, while preserving relative
          indentation.



          ...



          For each non-blank line, min leading white space characters are
          removed. Each white space character is treated as a single character. In
          particular, the tab character "t" (U+0009) is considered a
          single character; it is not expanded.



          Leading and trailing blank lines, if any, are removed. Trailing spaces are
          preserved.



          Each line is suffixed with a line feed character "n" (U+000A).




          To use this method, we can change the code to the following:



          System.out.println(`
          Test 1
          Test 2
          Test 3
          `.align());


          Which outputs the following (suffixed with a line feed character, as stated by the documentation):



          Test 1
          Test 2
          Test 3





          share|improve this answer



















          • 2





            To add to it :- this can also be attained using align(0) where the argument takes in input the indentation whitespaces equivalent then to align().indent(0)

            – nullpointer
            Nov 18 '18 at 7:48






          • 1





            Bad that the String API Javadoc doesn't list it still.

            – nullpointer
            Nov 18 '18 at 7:50






          • 3





            So you embed a rather large constant string into your code, just to perform an expensive operation on it after instantiation, to be repeated every time, this statement is executed. Compared to the good old "Test 1n" + " Test 2n" + " Test 3n" approach, this looks like a step backwards…

            – Holger
            Nov 19 '18 at 9:49











          • @Holger Though not in much favor of the raw strings, yet the indentation in the example from the question is missing from that of yours. That fanciness if I could term it, is what the new feature is adding to. Thoughts?

            – nullpointer
            Nov 19 '18 at 16:56








          • 1





            @Holger "this looks like a step backwards" - this will be solved by openjdk.java.net/jeps/303

            – ZhekaKozlov
            Dec 4 '18 at 14:02


















          8














          Accompanying Raw String Literals as a preview feature in Java 12 are new methods that will be added to java.lang.String, one of which is String#align. Its documentation states the following:




          Removes vertical and horizontal white space margins from around the
          essential body of a multi-line string, while preserving relative
          indentation.



          ...



          For each non-blank line, min leading white space characters are
          removed. Each white space character is treated as a single character. In
          particular, the tab character "t" (U+0009) is considered a
          single character; it is not expanded.



          Leading and trailing blank lines, if any, are removed. Trailing spaces are
          preserved.



          Each line is suffixed with a line feed character "n" (U+000A).




          To use this method, we can change the code to the following:



          System.out.println(`
          Test 1
          Test 2
          Test 3
          `.align());


          Which outputs the following (suffixed with a line feed character, as stated by the documentation):



          Test 1
          Test 2
          Test 3





          share|improve this answer



















          • 2





            To add to it :- this can also be attained using align(0) where the argument takes in input the indentation whitespaces equivalent then to align().indent(0)

            – nullpointer
            Nov 18 '18 at 7:48






          • 1





            Bad that the String API Javadoc doesn't list it still.

            – nullpointer
            Nov 18 '18 at 7:50






          • 3





            So you embed a rather large constant string into your code, just to perform an expensive operation on it after instantiation, to be repeated every time, this statement is executed. Compared to the good old "Test 1n" + " Test 2n" + " Test 3n" approach, this looks like a step backwards…

            – Holger
            Nov 19 '18 at 9:49











          • @Holger Though not in much favor of the raw strings, yet the indentation in the example from the question is missing from that of yours. That fanciness if I could term it, is what the new feature is adding to. Thoughts?

            – nullpointer
            Nov 19 '18 at 16:56








          • 1





            @Holger "this looks like a step backwards" - this will be solved by openjdk.java.net/jeps/303

            – ZhekaKozlov
            Dec 4 '18 at 14:02
















          8












          8








          8







          Accompanying Raw String Literals as a preview feature in Java 12 are new methods that will be added to java.lang.String, one of which is String#align. Its documentation states the following:




          Removes vertical and horizontal white space margins from around the
          essential body of a multi-line string, while preserving relative
          indentation.



          ...



          For each non-blank line, min leading white space characters are
          removed. Each white space character is treated as a single character. In
          particular, the tab character "t" (U+0009) is considered a
          single character; it is not expanded.



          Leading and trailing blank lines, if any, are removed. Trailing spaces are
          preserved.



          Each line is suffixed with a line feed character "n" (U+000A).




          To use this method, we can change the code to the following:



          System.out.println(`
          Test 1
          Test 2
          Test 3
          `.align());


          Which outputs the following (suffixed with a line feed character, as stated by the documentation):



          Test 1
          Test 2
          Test 3





          share|improve this answer













          Accompanying Raw String Literals as a preview feature in Java 12 are new methods that will be added to java.lang.String, one of which is String#align. Its documentation states the following:




          Removes vertical and horizontal white space margins from around the
          essential body of a multi-line string, while preserving relative
          indentation.



          ...



          For each non-blank line, min leading white space characters are
          removed. Each white space character is treated as a single character. In
          particular, the tab character "t" (U+0009) is considered a
          single character; it is not expanded.



          Leading and trailing blank lines, if any, are removed. Trailing spaces are
          preserved.



          Each line is suffixed with a line feed character "n" (U+000A).




          To use this method, we can change the code to the following:



          System.out.println(`
          Test 1
          Test 2
          Test 3
          `.align());


          Which outputs the following (suffixed with a line feed character, as stated by the documentation):



          Test 1
          Test 2
          Test 3






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 2:51









          Jacob G.Jacob G.

          15.3k52262




          15.3k52262








          • 2





            To add to it :- this can also be attained using align(0) where the argument takes in input the indentation whitespaces equivalent then to align().indent(0)

            – nullpointer
            Nov 18 '18 at 7:48






          • 1





            Bad that the String API Javadoc doesn't list it still.

            – nullpointer
            Nov 18 '18 at 7:50






          • 3





            So you embed a rather large constant string into your code, just to perform an expensive operation on it after instantiation, to be repeated every time, this statement is executed. Compared to the good old "Test 1n" + " Test 2n" + " Test 3n" approach, this looks like a step backwards…

            – Holger
            Nov 19 '18 at 9:49











          • @Holger Though not in much favor of the raw strings, yet the indentation in the example from the question is missing from that of yours. That fanciness if I could term it, is what the new feature is adding to. Thoughts?

            – nullpointer
            Nov 19 '18 at 16:56








          • 1





            @Holger "this looks like a step backwards" - this will be solved by openjdk.java.net/jeps/303

            – ZhekaKozlov
            Dec 4 '18 at 14:02
















          • 2





            To add to it :- this can also be attained using align(0) where the argument takes in input the indentation whitespaces equivalent then to align().indent(0)

            – nullpointer
            Nov 18 '18 at 7:48






          • 1





            Bad that the String API Javadoc doesn't list it still.

            – nullpointer
            Nov 18 '18 at 7:50






          • 3





            So you embed a rather large constant string into your code, just to perform an expensive operation on it after instantiation, to be repeated every time, this statement is executed. Compared to the good old "Test 1n" + " Test 2n" + " Test 3n" approach, this looks like a step backwards…

            – Holger
            Nov 19 '18 at 9:49











          • @Holger Though not in much favor of the raw strings, yet the indentation in the example from the question is missing from that of yours. That fanciness if I could term it, is what the new feature is adding to. Thoughts?

            – nullpointer
            Nov 19 '18 at 16:56








          • 1





            @Holger "this looks like a step backwards" - this will be solved by openjdk.java.net/jeps/303

            – ZhekaKozlov
            Dec 4 '18 at 14:02










          2




          2





          To add to it :- this can also be attained using align(0) where the argument takes in input the indentation whitespaces equivalent then to align().indent(0)

          – nullpointer
          Nov 18 '18 at 7:48





          To add to it :- this can also be attained using align(0) where the argument takes in input the indentation whitespaces equivalent then to align().indent(0)

          – nullpointer
          Nov 18 '18 at 7:48




          1




          1





          Bad that the String API Javadoc doesn't list it still.

          – nullpointer
          Nov 18 '18 at 7:50





          Bad that the String API Javadoc doesn't list it still.

          – nullpointer
          Nov 18 '18 at 7:50




          3




          3





          So you embed a rather large constant string into your code, just to perform an expensive operation on it after instantiation, to be repeated every time, this statement is executed. Compared to the good old "Test 1n" + " Test 2n" + " Test 3n" approach, this looks like a step backwards…

          – Holger
          Nov 19 '18 at 9:49





          So you embed a rather large constant string into your code, just to perform an expensive operation on it after instantiation, to be repeated every time, this statement is executed. Compared to the good old "Test 1n" + " Test 2n" + " Test 3n" approach, this looks like a step backwards…

          – Holger
          Nov 19 '18 at 9:49













          @Holger Though not in much favor of the raw strings, yet the indentation in the example from the question is missing from that of yours. That fanciness if I could term it, is what the new feature is adding to. Thoughts?

          – nullpointer
          Nov 19 '18 at 16:56







          @Holger Though not in much favor of the raw strings, yet the indentation in the example from the question is missing from that of yours. That fanciness if I could term it, is what the new feature is adding to. Thoughts?

          – nullpointer
          Nov 19 '18 at 16:56






          1




          1





          @Holger "this looks like a step backwards" - this will be solved by openjdk.java.net/jeps/303

          – ZhekaKozlov
          Dec 4 '18 at 14:02







          @Holger "this looks like a step backwards" - this will be solved by openjdk.java.net/jeps/303

          – ZhekaKozlov
          Dec 4 '18 at 14:02




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357469%2fraw-string-literals-remove-leading-indentation%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

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)