Home  >  Article  >  Java  >  New String API added in Java 11: How to use the strip() method to remove spaces at both ends of a string

New String API added in Java 11: How to use the strip() method to remove spaces at both ends of a string

WBOY
WBOYOriginal
2023-07-31 22:12:21854browse

Some new String APIs have been added to Java 11, including a very convenient method strip(), which can be used to remove spaces at both ends of a string. In this article, we will discuss how to use the strip() method and provide some sample code.

In previous Java versions, we usually used the trim() method to remove spaces at both ends of a string. However, the trim() method can only remove leading and trailing spaces from a string, and does not work for other Unicode whitespace characters.

The strip() method was introduced in Java 11 as a replacement for trim(). It uses Unicode's whitespace character definition and therefore can remove more types of whitespace characters.

The following is a simple example that shows how to use the strip() method to remove spaces at both ends of a string:

String str = "   Hello, World!   ";
String trimmedStr = str.strip();
System.out.println(trimmedStr);

Run the above code, the output will be "Hello, World!", Note that the spaces at both ends of the string have been removed.

In addition to the strip() method, Java 11 also provides several other methods to handle different needs: stripLeading(), stripTrailing(), and stripIndent(). They will be introduced one by one below.

  • stripLeading(): This method is used to remove spaces at the beginning of the string.
String str = "   Hello, World!";
String trimmedStr = str.stripLeading();
System.out.println(trimmedStr);

Run the above code, the output will be "Hello, World!", the leading spaces have been removed.

  • stripTrailing(): This method is used to remove spaces at the end of the string.
String str = "Hello, World!   ";
String trimmedStr = str.stripTrailing();
System.out.println(trimmedStr);

Run the above code, the output will be "Hello, World!", the trailing spaces have been removed.

  • stripIndent(): This method is used to remove the same indent at the beginning of each line of a multi-line string.
String str = """
        Hello,
        World!
        """;
String trimmedStr = str.stripIndent();
System.out.println(trimmedStr);

Run the above code, the output will be:

Hello,
World!

The indentation at the beginning of each line has been removed.

It should be noted that these methods return a new string object, and the original string object will not be modified. Because strings are immutable in Java, a new string object is generated every time a string is manipulated.

The newly added strip() method and other related methods in Java 11 can greatly simplify the string processing process. Whether it is removing spaces from both ends of a string or removing indentation from a multi-line string, these methods can help us accomplish the task easily.

To summarize, this article introduces the strip() method in the newly added String API in Java 11 and the use of related methods. They provide a more convenient and flexible way to process strings, allowing us to process string-related operations more efficiently. Whether it is removing spaces at both ends of a string or removing the indentation of a multi-line string, these methods are very practical. If you are using Java 11 or newer, we strongly recommend that you use these new String API methods to improve your string processing code.

The above is the detailed content of New String API added in Java 11: How to use the strip() method to remove spaces at both ends of a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]