Trim() vs Strip() in Java

Aziz Marzouki
3 min readOct 23, 2024

--

In Java, both trim() and strip() are methods used to remove whitespace from a string, but they differ in their functionality and the types of whitespace they handle.

Here's a detailed comparison of the two methods:

1. trim() Method

  • Definition: The trim() method removes leading and trailing whitespace from a string. Whitespace includes spaces, tabs, and newline characters.
  • Behavior: It uses the default Unicode values for whitespace. Any character defined as whitespace in Unicode is trimmed.
  • Return Type: Returns a new string with the whitespace removed, leaving the original string unchanged.

Example of trim()

public class TrimExample {
public static void main(String[] args) {
String originalString = " Hello, World! ";
// Using trim() to remove leading and trailing whitespace
String trimmedString = originalString.trim();
System.out.println("Original: '" + originalString + "'"); // Output: ' Hello, World! '
System.out.println("Trimmed: '" + trimmedString + "'"); // Output: 'Hello, World!'
}
}

2. strip() Method

  • Definition: The strip() method was introduced in Java 11. It also removes leading and trailing whitespace but uses a more comprehensive approach based on Unicode.
  • Behavior: It handles all Unicode whitespace characters, which may include characters not considered whitespace by the trim() method. This makes strip() more robust for internationalization and different text encodings.
  • Return Type: Returns a new string with the whitespace removed, just like trim().

Example of strip()

public class StripExample {
public static void main(String[] args) {
String originalString = " Hello, World! ";
// Using strip() to remove leading and trailing whitespace
String strippedString = originalString.strip();
System.out.println("Original: '" + originalString + "'"); // Output: ' Hello, World! '
System.out.println("Stripped: '" + strippedString + "'"); // Output: 'Hello, World!'
}
}

3. Differences Between trim() and strip()

Featuretrim()strip()Introduced inEarlier versions (before Java 11)Java 11Whitespace RemovalRemoves whitespace defined by ASCII (spaces, tabs, newlines)Removes all Unicode whitespace charactersUnicode HandlingLimited to ASCII whitespaceComprehensive Unicode supportReturn ValueNew string with whitespace removedNew string with whitespace removed

4. Performance

  • Generally, both methods are efficient for most use cases. However, since strip() handles more cases (especially for Unicode characters), it may be slightly slower than trim() in specific scenarios.

5. Conclusion

  • Use trim() for basic whitespace removal when working with ASCII strings and if you are targeting versions of Java before 11.
  • Use strip() for a more comprehensive solution that handles all Unicode whitespace characters and when working with Java 11 or later.

Example Comparison

To illustrate the difference between the two methods regarding Unicode whitespace, consider the following example:

public class TrimStripExample {
public static void main(String[] args) {
String unicodeString = "\u2005Hello, World!\u2005"; // Unicode whitespace (figure space)
String trimmed = unicodeString.trim();
String stripped = unicodeString.strip();
System.out.println("Original: '" + unicodeString + "'"); // Output: ' Hello, World! '
System.out.println("Trimmed: '" + trimmed + "'"); // Output: ' Hello, World! ' (doesn't remove unicode space)
System.out.println("Stripped: '" + stripped + "'"); // Output: 'Hello, World!' (removes all types of whitespace)
}
}

In this example, you can see that trim() does not remove the Unicode whitespace character, while strip() successfully removes it.

--

--

Aziz Marzouki
Aziz Marzouki

Written by Aziz Marzouki

Software Engineer specializing in full-stack web development and DevOps, passionate about problem-solving and writing.

No responses yet