Indentation and Stripping
Control incidental whitespace.
Indentation and Stripping is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Incidental vs Essential Whitespace
When you indent a text block to match your code, that indentation is incidental and should not be part of the string. Java strips it automatically, keeping only essential whitespace.
The Closing Delimiter Sets the Margin
Java looks at all content lines and the closing """ to find the common leftmost non-blank column. Everything to the left of that margin is removed.
public class Main {
public static void main(String[] args) {
String text = """
Hello
World
""";
System.out.println("[" + text + "]");
}
}Moving the Closing Quotes Left
If the closing """ sits further left than the content, the extra leading spaces on each line are kept as essential whitespace.
public class Main {
public static void main(String[] args) {
String text = """
Hello
World
""";
System.out.print(text);
}
}Trailing Whitespace Is Removed
Spaces at the end of each line are stripped automatically. This prevents invisible trailing-space bugs that plague ordinary strings.
public class Main {
public static void main(String[] args) {
String text = """
line one
line two
""";
for (String line : text.split("\n")) {
System.out.println("<" + line + ">");
}
}
}The stripIndent Method
The String.stripIndent() method applies the same incidental-whitespace algorithm to any string at runtime. It is what text blocks use under the hood.
public class Main {
public static void main(String[] args) {
String raw = " a\n b\n c\n";
System.out.print(raw.stripIndent());
}
}Controlling Indentation Inside
Indentation relative to the margin is preserved. Here the second line is indented more than the first, and that extra indent stays in the output.
public class Main {
public static void main(String[] args) {
String tree = """
root
child
grandchild
""";
System.out.print(tree);
}
}The indent Method
String.indent(int n) adds n spaces to the start of every line and ensures a trailing newline. It is handy for nesting blocks of text.
public class Main {
public static void main(String[] args) {
String text = """
a
b
""";
System.out.print(text.indent(4));
}
}strip vs trim
Use strip() to remove leading and trailing whitespace from the whole block. Unlike the older trim(), strip() is Unicode-aware.
public class Main {
public static void main(String[] args) {
String text = """
content
""";
System.out.println("[" + text.strip() + "]");
}
}Closing Quotes on Their Own Line
If the closing """ is on its own line, the string ends with a trailing newline. Place it right after the last character to drop that newline.
public class Main {
public static void main(String[] args) {
String withNewline = """
done
""";
String noNewline = """
done""";
System.out.println("A:" + withNewline.length());
System.out.println("B:" + noNewline.length());
}
}Aligning With Your Code
You can indent the whole block to match surrounding code for readability, and the incidental indentation disappears from the value.
public class Main {
static String menu() {
return """
Start
Options
Quit
""";
}
public static void main(String[] args) {
System.out.print(menu());
}
}Verifying the Margin
To debug stripping, wrap each line in markers. This shows exactly which leading spaces survived.
public class Main {
public static void main(String[] args) {
String block = """
x
y
""";
for (String line : block.split("\n")) {
System.out.println("|" + line + "|");
}
}
}Quick Check
What determines how much leading (incidental) whitespace is stripped from a text block?
Recap
You learned indentation and stripping:
- Incidental indentation is removed automatically
- The closing
"""position can set the margin - Trailing line whitespace is stripped
stripIndent(),indent(n), andstrip()help at runtime- Closing quotes placement controls the final newline
Frequently asked questions
Is the “Indentation and Stripping” lesson free?
Yes — the full text of “Indentation and Stripping” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Indentation and Stripping”?
Control incidental whitespace. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Indentation and Stripping” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Text Blocks
- Indentation and Stripping
- Escapes in Text Blocks
- Use Cases: JSON and SQL