Monday, October 6, 2025
HomeLanguagesJavaJava String Class strip() Method With Examples

Java String Class strip() Method With Examples

Java String Class strip() method returns a string that provides a string with all leading and trailing white spaces removed. This method is similar to the String.trim() method. There are basically 3 methods by which we can remove white spaces in a different manner.

strip() Method: It returns a string, with all leading and trailing white space removed

Syntax:

String strippedString = string.strip()

stripLeading() Method: It returns a string, with all leading white space removed.

Syntax:

String leadingStripString = string.stripLeading()

stripTrailing() Method: It returns a string, with all trailing white space removed.

Syntax:

String trailedStripString = string.stripTrailing()

Example:

Java




// Java Program to demonstrate the use of the strip()
// method,stripLeading() method,stripTrailing() method
  
public class GFG {
    public static void main(String[] args)
    {
        String str
            = "        Geeks For Geeks Internship    !   ";
  
        // removing leading and
        // trailing white spaces
        System.out.println(str.strip());
  
        // removing leading white spaces
        System.out.println(str.stripLeading());
  
        // removing trailing white spaces
        System.out.println(str.stripTrailing());
    }
}


Output:

Geeks For Geeks Internship    !
Geeks For Geeks Internship    !   
        Geeks For Geeks Internship    !
RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6780 POSTS0 COMMENTS