Raw String Literals - Remove Leading Indentation
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
add a comment |
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
add a comment |
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
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
java string indentation rawstring java-12
edited Dec 19 '18 at 23:55
Jacob G.
asked Nov 18 '18 at 2:51
Jacob G.Jacob G.
15.3k52262
15.3k52262
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
2
To add to it :- this can also be attained usingalign(0)
where the argument takes in input the indentation whitespaces equivalent then toalign().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
|
show 3 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
2
To add to it :- this can also be attained usingalign(0)
where the argument takes in input the indentation whitespaces equivalent then toalign().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
|
show 3 more comments
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
2
To add to it :- this can also be attained usingalign(0)
where the argument takes in input the indentation whitespaces equivalent then toalign().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
|
show 3 more comments
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
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
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 usingalign(0)
where the argument takes in input the indentation whitespaces equivalent then toalign().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
|
show 3 more comments
2
To add to it :- this can also be attained usingalign(0)
where the argument takes in input the indentation whitespaces equivalent then toalign().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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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