Which of the following is a new method added to the String class in Java 11?

Java MCQ: Which of the following is a new method added to the String class in Java 11?

a) trim()
b) strip()
c) substring()
d) replace()

Answer:

b) strip()

Explanation:

The strip() method is one of the new methods added to the String class in Java 11. This method removes all leading and trailing whitespace from the string. Unlike trim(), which only removes spaces (ASCII 32), strip() also removes all kinds of whitespace characters, including non-breaking spaces and other Unicode whitespace characters.

Here’s an example of using strip():

public class StringStripExample {
    public static void main(String[] args) {
        String str = "\u2001\u2002Hello World\u2003";
        System.out.println("Before strip: [" + str + "]");
        System.out.println("After strip: [" + str.strip() + "]");
    }
}

In this example, strip() removes all leading and trailing whitespace characters, including Unicode spaces, from the string str.

Java 11 also introduced other useful methods in the String class, such as lines(), repeat(), and isBlank(), which provide additional functionality for string manipulation.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top