Java String stripLeading() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 11+

Definition

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();
CallWhat 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

ParameterDescription
NonestripLeading() takes no parameters.

Return Value

Return ValueDescription
StringThe 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 + "]");
    }
}

How It Works

  1. The start is cleaned. The three leading spaces are gone, so the text now begins with l.
  2. The end is untouched. The three trailing spaces are still visible before the closing bracket.
  3. 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());
        }
    }
}

How It Works

  1. Indentation is leading white space. Each nested level adds four spaces at the start of the line.
  2. Length difference. Subtracting the stripped length from the full length counts exactly those spaces: 0, 4, 8, 4, 0.
  3. 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() + "]");
    }
}

How It Works

  1. parseInt is strict. Passing "\t 42" directly would throw NumberFormatException.
  2. Strip, then parse. After stripLeading() the text is "42", which parses, and adding 8 prints 50.
  3. 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.
Compute line.length() - line.stripLeading().length().

Next: stripTrailing()

Remove Unicode whitespace from the end only (Java 11+).

stripTrailing() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful