In the below code, we define three strings. One has only white spaces, one has leading and trailing white spaces with characters in it, and one has only trailing white spaces. The output will be as follows.
" " - ""
" hello " - "hello"
"h i " - "h i"
public class Main {
public static void main(String[] args) {
String s = " ";
System.out.println("\"" + s + "\" - \"" + s.strip() + "\"");
s = " hello ";
System.out.println("\"" + s + "\" - \"" + s.strip() + "\"");
s = "h i ";
System.out.println("\"" + s + "\" - \"" + s.strip() + "\"");
}
}