Open In App
Related Articles

String Class stripLeading() Method in Java With Examples

Improve Article
Save Article
Like Article

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. 



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


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


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 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 + "#");
        // using strip() method
        System.out.println("Using strip()");
        System.out.println("#" + str.strip() + "#");
        // using stripLeading() method
        System.out.println("Using stripLeading()");
        System.out.println("#" + str.stripLeading() + "#");
        // using stripTrailing() method
        System.out.println("Using stripTrailing()");
        System.out.println("#" + str.stripTrailing() + "#");


String is
#  Hello,  World #

Using strip()
#Hello,  World#

Using stripLeading()
#Hello,  World #

Using stripTrailing()
#  Hello,  World#
Last Updated : 05 Feb, 2021
Like Article
Save Article
Similar Reads
Related Tutorials