tag:blogger.com,1999:blog-8712770457197348465.post5280096427391808694..comments2024-03-19T01:04:48.783-07:00Comments on Javarevisited: 5 ways to check if String is empty in Java - examplesjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-8712770457197348465.post-58815675354184198662017-01-31T15:13:09.158-08:002017-01-31T15:13:09.158-08:00Hello Ranjit, it doesn't work because "&q...Hello Ranjit, it doesn't work because "" is a String and not character.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-40230456508133023512017-01-30T07:14:03.777-08:002017-01-30T07:14:03.777-08:00Why does if(char z=="") does not work?
G...Why does if(char z=="") does not work?<br />Give some condition to check the blank in stringAnonymoushttps://www.blogger.com/profile/06046358480011628557noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25947528990772952582015-10-01T07:43:05.322-07:002015-10-01T07:43:05.322-07:00@Anonymous, that's true, the length() will ret...@Anonymous, that's true, the length() will return non-zero value for String containing only space, if you consider it empty, which many of us do, then trim() is the right option. In SQL, you need to both rtrim() and ltrim(), thank god, Java trim() removes both leading and trailing space :-)javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24987693041701572502015-10-01T07:31:05.026-07:002015-10-01T07:31:05.026-07:00I have used your first example thousands of times,...I have used your first example thousands of times,<br />if(string != null && string.length() == 0){ return true; }<br /><br />I got burned. The string was all whitespace (and had length > 0). I recommend removing the whitespace before checking the length using the String trim() method<br />,<br />if(string != null && string.trim().length() == 0){ return true; }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28161821404206723802013-03-04T01:14:58.379-08:002013-03-04T01:14:58.379-08:00What is difference between empty String and blank ...What is difference between empty String and blank String?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-8629966061928419222013-02-22T08:12:37.266-08:002013-02-22T08:12:37.266-08:00Another option to StringUtils.isBlank(" &quo...Another option to StringUtils.isBlank(" "); could be " ".trim().length() == 0Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-39686312462766375462013-02-22T03:51:09.146-08:002013-02-22T03:51:09.146-08:00I recommend Guava and its class com.google.common....I recommend Guava and its class com.google.common.base.Strings. It contains methods isNullOrEmpty(String string). Full docs here:<br />http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Strings.html#isNullOrEmpty(java.lang.String)<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26443680723738576792013-02-04T16:58:26.483-08:002013-02-04T16:58:26.483-08:00@Anonymous, isBlank() and isNotBlank() is also a g...@Anonymous, isBlank() and isNotBlank() is also a good choice but they return true even if String contains whitespace, i.e. they are not empty or you can say when length of String is not zero. If your definition of empty String is equal to blank i.e. include white space than isBlank() is a real good choice.<br /><br /> StringUtils.isBlank(null) = true<br /> StringUtils.isBlank("")Javin @ ClassLoader in Javahttp://javarevisited.blogspot.com/2012/12/how-classloader-works-in-java.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37641224523307201022013-02-04T04:10:24.363-08:002013-02-04T04:10:24.363-08:00I also prefer using apache's StringUtils isBla...I also prefer using apache's StringUtils isBlank() and isNotBlank().Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47246060256330670212013-02-01T05:24:39.185-08:002013-02-01T05:24:39.185-08:00I don't think using StringUtils from Spring fr...I don't think using StringUtils from Spring framework should be considered best practice, because in their documentation is:<br /><br />Mainly for internal use within the framework; consider Jakarta's Commons Lang for a more comprehensive suite of String utilities.<br /><br />see: <a href="http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/util/Jiri Pinkashttp://www.javavids.comnoreply@blogger.com