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
class GFG {
public static void main(String[] args)
{
String str = " Hello, World " ;
System.out.println( "String is" );
System.out.println( "#" + str + "#" );
System.out.println();
System.out.println( "Using strip()" );
System.out.println( "#" + str.strip() + "#" );
System.out.println();
System.out.println( "Using stripLeading()" );
System.out.println( "#" + str.stripLeading() + "#" );
System.out.println();
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#