Sunday, November 23, 2025
HomeLanguagesJavaString Class stripLeading() Method in Java With Examples

String Class stripLeading() Method in Java With Examples

String Class stripLeading() method is used to strip leading whitespaces from the string i.e stripLeading() method removes all the whitespaces only at the starting of the string. 

Example:

Input:

String name = "   kapil   ";
System.out.println("#" + name + "#);
System.out.println("#" + name.stripLeading());


Output:

#   kapil   #
#kapil   # // Leading whitespaces are removed 

Syntax:

public String stripLeading()

Returns: The string after removing all the whitespaces at the start of the string.

Note: Like the stripTrailing() method in java, we have strip() (removes leading and trailing whitespace) and stripLeading() (removes the only leading whitespace). 

Below is the implementation of the problem statement:

Java




// Java program to demonstrate the usage of
// stripLeading() method in comparison to
// other methods
  
class GFG {
    public static void main(String[] args)
    {
  
        // creating a string
        String str = " Hello, World ";
  
        // print the string without any function
        System.out.println("String is");
        System.out.println("#" + str + "#");
        System.out.println();
  
        // using strip() method
        System.out.println("Using strip()");
        System.out.println("#" + str.strip() + "#");
        System.out.println();
  
        // using stripLeading() method
        System.out.println("Using stripLeading()");
        System.out.println("#" + str.stripLeading() + "#");
        System.out.println();
  
        // using stripTrailing() method
        System.out.println("Using stripTrailing()");
        System.out.println("#" + str.stripTrailing() + "#");
    }
}


Output:

String is
#  Hello,  World #

Using strip()
#Hello,  World#

Using stripLeading()
#Hello,  World #

Using stripTrailing()
#  Hello,  World#
RELATED ARTICLES

Most Popular

Dominic
32410 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6909 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6865 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS