Java String stripTrailing() Method

The String.stripTrailing() method in Java is used to remove trailing whitespace from a string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. stripTrailing Method Syntax
  3. Examples
    • Removing Trailing Whitespace
    • Comparison with trim()
    • Handling Strings Without Trailing Whitespace
  4. Conclusion

Introduction

The String.stripTrailing() method is a member of the String class in Java, introduced in Java 11. It allows you to remove trailing whitespace from a string, similar to the trim() method but with more precise Unicode whitespace handling.

stripTrailing Method Syntax

The syntax for the stripTrailing method is as follows:

public String stripTrailing()

Examples

Removing Trailing Whitespace

The stripTrailing method can be used to remove trailing whitespace from a string.

Example

public class StripTrailingExample {
    public static void main(String[] args) {
        String message = "   Hello, World!   ";

        String trailingStrippedMessage = message.stripTrailing();

        System.out.println("Original message: '" + message + "'");
        System.out.println("Trailing stripped message: '" + trailingStrippedMessage + "'");
    }
}

Output:

Original message: '   Hello, World!   '
Trailing stripped message: '   Hello, World!'

Comparison with trim()

The stripTrailing method handles Unicode whitespace more precisely compared to the trim() method, which only removes ASCII whitespace. However, since trim() removes both leading and trailing whitespace, this example will focus on the trailing whitespace removal.

Example

public class StripTrailingExample {
    public static void main(String[] args) {
        String message = "   Hello, World!\u2000\u2000";  // Unicode whitespace

        String trimmedMessage = message.trim();
        String trailingStrippedMessage = message.stripTrailing();

        System.out.println("Original message: '" + message + "'");
        System.out.println("Trimmed message: '" + trimmedMessage + "'");
        System.out.println("Trailing stripped message: '" + trailingStrippedMessage + "'");
    }
}

Output:

Original message: '   Hello, World!  '
Trimmed message: 'Hello, World!  '
Trailing stripped message: '   Hello, World!'

Handling Strings Without Trailing Whitespace

The stripTrailing method will return the original string if there is no trailing whitespace.

Example

public class StripTrailingExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        String trailingStrippedMessage = message.stripTrailing();

        System.out.println("Original message: '" + message + "'");
        System.out.println("Trailing stripped message: '" + trailingStrippedMessage + "'");
    }
}

Output:

Original message: 'Hello, World!'
Trailing stripped message: 'Hello, World!'

Conclusion

The String.stripTrailing() method in Java is used for removing trailing whitespace from a string. By understanding how to use this method, you can efficiently clean and format strings in your Java applications. Whether you are handling strings with trailing whitespace or comparing it with the trim() method, the stripTrailing method provides a reliable solution for managing whitespace at the end of a string.

Comments