Javatpoint Logo
Javatpoint Logo

String.strip() Method in Java

A basic and often used class in Java that represents character sequences is called String. It offers several ways to work with and alter strings. strip() is one of the comparatively more recent methods added in Java 11.

Java String.strip() Method

To eliminate leading and trailing white spaces from a string, use the strip() method. It is an easy approach to handle strings or clean up user inputs without requiring extra steps or intricate reasoning.


The strip() method does not take any parameters and returns a new string with leading and trailing white spaces removed.


Original String: '   Welcome to JavaTpoint   '
Stripped String: 'Welcome to JavaTpoint'


In this instance, the inputString, which contains leading and trailing spaces, is subjected to the strip() method. These spaces would not be present in the strippedString that is produced.

Flavours of strip() Methods

Apart from strip(), Java provides other methods that offer similar functionality:

stripLeading(): The method removes only the leading white spaces.

stripTrailing(): The method removes only the trailing white spaces.

Let's use the above method in a Java program.


Original String: '   Welcome to JavaTpoint   '
Stripped Leading: 'Welcome to JavaTpoint   '
Stripped Trailing: '   Welcome to JavaTpoint'

Uses of strip() Method

1. Whitespace Character

The term "whitespace character" describes characters that take up space but are invisible, meaning they cannot be printed. Newline, tab, and space characters are examples of common whitespace characters. The leading and trailing instances of these characters are the main focus of the strip() method.

2. Unicode

Java strings are based on Unicode, which is a character encoding standard that assigns a unique number to each character across different writing systems. Whitespace characters in Unicode include space (' '), tab ('\t'), line feed ('\n'), carriage return ('\r'), and others. The strip() method takes Unicode whitespace into account when removing leading and trailing spaces.

3. String Immutability

Strings formed in Java are immutable, which means they cannot be altered once they are created. Rather than altering the original string, the strip() method follows this idea by returning a new string with leading and trailing whitespace removed.

4. Handling Null Strings

The strip() function will throw a NullPointerException if it is called on a null string. To prevent runtime issues, make sure the string is not null before using the strip() method.

5. Comparison with trim() Method

Java's trim() function, which was available in previous versions, also eliminates leading and trailing whitespaces. Nevertheless, trim() and strip() differ in that the former simply eliminates ASCII whitespace (space, tab, and newline), while the latter takes into account all Unicode whitespace characters.

6. Performance Considerations

It's important to take performance into account when working with huge amounts of data or often executing string manipulations. The temporal complexity of the strip() method is related to the length of the string, just like other string operations.

7. User Input Validation

It's typical for users to inadvertently add leading or trailing whitespaces while interacting with user inputs in applications. By using strip(), you can be guaranteed that the input is error-free and won't interfere with validation.


Java string manipulation chores can be made much simpler by learning how to use the strip() method and its variations in the String class. When handling leading and trailing whitespaces, these methods offer a clear and efficient solution, regardless of whether you are handling user input, reading from files, or processing data.

Recall that accurate data processing requires clean strings, and Java's strip() methods provide an easy way to accomplish this. Java's strip() method is a useful addition that offers a more thorough and organized solution than its predecessors for managing leading and trailing whitespaces in strings.

It complies with the string immutability, Unicode support, and effective string manipulation tenets of Java. Understanding these methods for string manipulation as a Java developer improves your capacity to handle and process textual data efficiently.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA