String str1 = " hello world "; String result1 = StringUtils.trimToNull(str1); System.out.println(result1); // "hello world" String str2 = " "; String result2 = StringUtils.trimToNull(str2); System.out.println(result2); // null
String input = null; String result = StringUtils.trimToNull(input); System.out.println(result); // nullIn this example, we pass a null string to trimToNull, which returns null. The StringUtils class belongs to the org.apache.commons.lang3 package of the Apache Commons Lang3 library.