What is String.stripLeading in Java?
What is stripLeading()?
stripLeading() is an instance method that returns a string with all leading white spaces removed from the value.
Leading means it is at the beginning of the string. This method was introduced in Java 11.
If a string contains only white spaces, then applying this method results in an empty string.
Code
Let’s look at the following code example which shows how to implement stripLeading() in a Java program:
class Main {public static void main(String[] args) {String s = " hello";System.out.println("" + s.stripLeading());}}
Explanation
In the code above, we defined a string with leading and trailing white spaces. On applying the stripLeading() method, only the leading white spaces are removed.
The code will print "hello " and exit.