8

Given the following code

public class TextBlock {

    public static void main(String[] args) {
        String indentedText = """
            hello
                indented
            world
        """;
        System.out.println(indentedText);
    }
}

The output is as follows (mind the leading spaces):

    hello
        indented
    world

How to obtain String value like below (without unnecessary leading spaces)?

hello
    indented
world
1

1 Answer 1

19

You can manipulate indentation by changing closing quotes position in the code ("""). For example

String indentedText = """
                hello
                    indented
                world
    """;
System.out.println(indentedText);

Would produce

        hello
            indented
        world

but

String indentedText = """
                hello
                    indented
                world
                """;
System.out.println(indentedText);

will produce

hello
    indented
world
Sign up to request clarification or add additional context in comments.

1 Comment

@WojciechWirzibicki the output for your first example should be indented by 12 spaces instead of 8. Perhaps you could edit your answer to show this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.