Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
strip
space

What are the strip() methods in Java?

Programming Bytes

In Java 11, there are three new methods to remove whitespaces from a string.

  • The stripLeading method will remove all whitespaces from the beginning of a string.
  • The stripTrailing method will remove all whitespaces from the end of a string.
  • The strip method 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.

RELATED TAGS

java
strip
space
RELATED COURSES

View all Courses

Keep Exploring