In this code, the strip() method is used to remove leading and trailing whitespace from strings. The strip() method, introduced in Java 11, is Unicode-aware and removes all leading and trailing characters that are considered whitespace according to the Unicode standard.
docs.oracle.com
Analysis of Each Statement:
First Statement:
java
String s = " ";
System.out.print("[" + s.strip());
The string s contains four spaces.
Applying s.strip() removes all leading and trailing spaces, resulting in an empty string.
The output is "[" followed by the empty string, so the printed result is "[".
Second Statement:
java
s = " hello ";
System.out.print("," + s.strip());
The string s is now " hello ".
Applying s.strip() removes all leading and trailing spaces, resulting in "hello".
The output is "," followed by "hello", so the printed result is ",hello".
Third Statement:
java
s = "h i ";
System.out.print("," + s.strip() + "]");
The string s is now "h i ".
Applying s.strip() removes the trailing spaces, resulting in "h i".
The output is "," followed by "h i" and then "]", so the printed result is ",h i]".
Combined Output:
Combining all parts, the final output is:
css
[,hello,h i]
[Reference:, String (Java SE 11 & JDK 11), , ]
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit