Java String stripLeading()

In this guide, you will learn about the String stripLeading() method in Java programming and how to use it with an example.

1. stripLeading() Method Overview


The stripLeading() method is part of the String class in Java and was introduced in Java 11. This method is used to remove any leading whitespace from a string according to the Unicode Character Property “White_Space”. Unlike the trim() method, which only removes leading spaces, stripLeading() can remove any Unicode whitespace character.


public String stripLeading()


The method does not take any parameters.

Key Points:

- It removes all types of leading whitespaces as defined by the Unicode standard.

- If the string has no leading whitespaces, the same string is returned.

- It does not affect trailing whitespaces.

2. stripLeading() Method Example

public class StripLeadingExample {
    public static void main(String[] args) {
        String str = " \t  \u2005Java\u2005   \n";
        String strippedString = str.stripLeading();
        System.out.println("Original String: [" + str + "]");
        System.out.println("String with Leading Whitespaces Stripped: [" + strippedString + "]");


Original String: [ 	  Java 
String with Leading Whitespaces Stripped: [Java 


In this example, the str string contains various types of whitespaces including tabs, spaces, and the Unicode character \u2005 (Four-Per-Em Space) around the word "Java". 

The stripLeading() method is called, and all the leading whitespaces are removed. However, the trailing whitespaces remain unaffected. The modified string is then printed to the console.