String str = " Hello World "; String strTrimmed = str.trim(); System.out.println(strTrimmed); // Output: "Hello World"
String str = " \t\n "; String strTrimmed = str.trim(); System.out.println(strTrimmed.isEmpty()); // Output: trueIn this example, we have a string that contains only white spaces and newlines. We use the trim() method to remove these white spaces and newlines. Since the resulting string is empty, we can use the isEmpty() method to check if it is empty. The java.lang String trim() method is part of the java.lang package library, which is automatically included in every Java program.