What are the strip() methods in Java?
In Java 11, there are three new methods to remove whitespaces from a string.
- The
stripLeadingmethod will remove all whitespaces from the beginning of a string. - The
stripTrailingmethod will remove all whitespaces from the end of a string. - The
stripmethod will remove all whitespaces from the beginning and end of a string.
public class StripSpaec {public static void main(String[] args){String str = " Hello World. \t\n";System.out.println("Original String =>" + str );System.out.println( "Use strip to remove all white space at begining and end =>" + str.strip() );System.out.println("Use stripLeading to remove white space at begining=>" + str.stripLeading() );System.out.println("Use stripTrailing to remove white space at end =>" + str.stripTrailing() );}}
You can test the above code here.