In Java, stripLeading() returns a string with all white space removed from the beginning; anything at the end is kept. White space means characters for which Character.isWhitespace is true.
It is the "left trim" Java long lacked. Pair it with stripTrailing() for the right side, or use strip() to clean both ends at once.
stripLeading() is an instance method of java.lang.String, added in Java 11. Call it on a string, as in text.stripLeading(); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result. Requires Java 11 or newer.
Syntax
Call it with no arguments.
java
String text = line.stripLeading();
int indent = line.length() - line.stripLeading().length();
Call
What it does
" a ".stripLeading()
"a ".
"\t\tcode".stripLeading()
"code".
"x".stripLeading()
"x", the same object.
Subtracting the stripped length from the original length tells you how many leading white space chars a line has: a handy way to measure indentation.
Library declarations
java
public String stripLeading();
Parameters
Parameter
Description
None
stripLeading() takes no parameters.
Return Value
Return Value
Description
String
The text without leading white space, or this string if it has none.
Java Examples
Example 1 — Strip Only the Left Side
Brackets reveal which spaces are left after the call.
java
public class StripLeadingBasic {
public static void main(String[] args) {
String s = " left and right ";
System.out.println("[" + s.stripLeading() + "]");
System.out.println("[" + s + "]");
}
}
Output
[left and right ]
[ left and right ]
How It Works
The start is cleaned. The three leading spaces are gone, so the text now begins with l.
The end is untouched. The three trailing spaces are still visible before the closing bracket.
The source keeps both.s itself still has spaces on both sides; only the returned copy changed.
Example 2 — Measure the Indentation of Code Lines
Compare each line's length before and after stripping the start.
java
public class StripLeadingCode {
public static void main(String[] args) {
String[] lines = {"class A {", " void run() {", " go();", " }", "}"};
for (String line : lines) {
int indent = line.length() - line.stripLeading().length();
System.out.println(indent + ": " + line.stripLeading());
}
}
}
Output
0: class A {
4: void run() {
8: go();
4: }
0: }
How It Works
Indentation is leading white space. Each nested level adds four spaces at the start of the line.
Length difference. Subtracting the stripped length from the full length counts exactly those spaces: 0, 4, 8, 4, 0.
Content without indentation. Printing the stripped line next to the count gives a compact outline of the code.
Example 3 — Parse a Number with Leading Spaces
Integer.parseInt rejects leading white space, so strip it first.
java
public class StripLeadingNumber {
public static void main(String[] args) {
String field = "\t 42";
int value = Integer.parseInt(field.stripLeading());
System.out.println(value + 8);
String padded = " 7 ";
System.out.println("[" + padded.stripLeading() + "]");
}
}
Output
50
[7 ]
How It Works
parseInt is strict. Passing "\t 42" directly would throw NumberFormatException.
Strip, then parse. After stripLeading() the text is "42", which parses, and adding 8 prints 50.
Only the left side. For " 7 " the trailing spaces remain; use strip() before parsing such fields.
Frequently Asked Questions
It removes white space from the start of the string only and returns the result as a new String.
Java 11, together with strip() and stripTrailing().
Use a regex: s.replaceAll("^\\s+", ""). It removes whitespace matched by \s at the start.
No. Use stripTrailing() for the end, or strip() for both ends.