StringUtils.isBlank() vs String.isEmpty()











up vote
127
down vote

favorite
34












I ran into some code that has the following:



String foo = getvalue("foo");
if (StringUtils.isBlank(foo))
doStuff();
else
doOtherStuff();


This appears to be functionally equivalent to the following:



String foo = getvalue("foo");
if (foo.isEmpty())
doStuff();
else
doOtherStuff();


Is a difference between the two (org.apache.commons.lang3.StringUtils.isBlank and java.lang.String.isEmpty)?










share|improve this question




















  • 6




    Might be worth mentioning that there's also a StringUtils.isEmpty(foo) which helps you avoid null pointers, just like isBlank, but doesn't check for whitespace characters.
    – Xavi
    Jun 24 '14 at 19:32















up vote
127
down vote

favorite
34












I ran into some code that has the following:



String foo = getvalue("foo");
if (StringUtils.isBlank(foo))
doStuff();
else
doOtherStuff();


This appears to be functionally equivalent to the following:



String foo = getvalue("foo");
if (foo.isEmpty())
doStuff();
else
doOtherStuff();


Is a difference between the two (org.apache.commons.lang3.StringUtils.isBlank and java.lang.String.isEmpty)?










share|improve this question




















  • 6




    Might be worth mentioning that there's also a StringUtils.isEmpty(foo) which helps you avoid null pointers, just like isBlank, but doesn't check for whitespace characters.
    – Xavi
    Jun 24 '14 at 19:32













up vote
127
down vote

favorite
34









up vote
127
down vote

favorite
34






34





I ran into some code that has the following:



String foo = getvalue("foo");
if (StringUtils.isBlank(foo))
doStuff();
else
doOtherStuff();


This appears to be functionally equivalent to the following:



String foo = getvalue("foo");
if (foo.isEmpty())
doStuff();
else
doOtherStuff();


Is a difference between the two (org.apache.commons.lang3.StringUtils.isBlank and java.lang.String.isEmpty)?










share|improve this question















I ran into some code that has the following:



String foo = getvalue("foo");
if (StringUtils.isBlank(foo))
doStuff();
else
doOtherStuff();


This appears to be functionally equivalent to the following:



String foo = getvalue("foo");
if (foo.isEmpty())
doStuff();
else
doOtherStuff();


Is a difference between the two (org.apache.commons.lang3.StringUtils.isBlank and java.lang.String.isEmpty)?







java string string-utils






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 22 '15 at 9:29









rmoestl

1,87321733




1,87321733










asked May 2 '14 at 0:51









NSA

1,64052334




1,64052334








  • 6




    Might be worth mentioning that there's also a StringUtils.isEmpty(foo) which helps you avoid null pointers, just like isBlank, but doesn't check for whitespace characters.
    – Xavi
    Jun 24 '14 at 19:32














  • 6




    Might be worth mentioning that there's also a StringUtils.isEmpty(foo) which helps you avoid null pointers, just like isBlank, but doesn't check for whitespace characters.
    – Xavi
    Jun 24 '14 at 19:32








6




6




Might be worth mentioning that there's also a StringUtils.isEmpty(foo) which helps you avoid null pointers, just like isBlank, but doesn't check for whitespace characters.
– Xavi
Jun 24 '14 at 19:32




Might be worth mentioning that there's also a StringUtils.isEmpty(foo) which helps you avoid null pointers, just like isBlank, but doesn't check for whitespace characters.
– Xavi
Jun 24 '14 at 19:32












8 Answers
8






active

oldest

votes

















up vote
217
down vote



accepted










StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.



From the linked documentation:




Checks if a String is whitespace, empty ("") or null.



 StringUtils.isBlank(null)      = true
StringUtils.isBlank("")        = true
StringUtils.isBlank(" ")       = true
StringUtils.isBlank("bob")     = false
StringUtils.isBlank("  bob  ") = false






share|improve this answer






























    up vote
    103
    down vote













    The accepted answer from @arshajii is totally correct. However just being more explicit by saying below,



    StringUtils.isBlank()



     StringUtils.isBlank(null)      = true
    StringUtils.isBlank("") = true
    StringUtils.isBlank(" ") = true
    StringUtils.isBlank("bob") = false
    StringUtils.isBlank(" bob ") = false


    StringUtils.isEmpty



     StringUtils.isEmpty(null)      = true
    StringUtils.isEmpty("") = true
    StringUtils.isEmpty(" ") = false
    StringUtils.isEmpty("bob") = false
    StringUtils.isEmpty(" bob ") = false





    share|improve this answer






























      up vote
      32
      down vote













      StringUtils isEmpty = String isEmpty checks + checks for null.



      StringUtils isBlank = StringUtils isEmpty checks + checks if the text contains only whitespace character(s).



      Useful links for further investigation:




      • StringUtils isBlank documentation

      • StringUtils isEmpty documentation

      • String isEmpty documentation






      share|improve this answer






























        up vote
        13
        down vote













        StringUtils.isBlank() will also check for null, whereas this:



        String foo = getvalue("foo");
        if (foo.isEmpty())


        will throw a NullPointerException if foo is null.






        share|improve this answer



















        • 4




          There is a bigger difference than that; see my answer.
          – arshajii
          May 2 '14 at 0:56






        • 1




          This is incorrect. String.isEmpty() will return true if it is null. At least if you are talking about the apache.commons.lang version. I'm not sure about the Spring version.
          – ryoung
          May 26 '15 at 13:52








        • 1




          The meaning of my comment was edited away (to be fair it could have been more clear from the get-go); I was not comparing StringUtils.isBlank() to StringUtils.isEmpty() - rather StringUtils.isBlank() to the OP's value.isEmpty()
          – chut
          May 27 '15 at 17:15










        • chut's answer is correct. If java String foo is null, then foo.isEmpty() throws NullPointerException. The apache StringUtils.isBlank(foo) returns true even if foo is null.
          – user2590805
          Oct 5 at 8:53


















        up vote
        6
        down vote













        StringUtils.isBlank also returns true for just whitespace:




        isBlank(String str)



        Checks if a String is whitespace, empty ("") or null.







        share|improve this answer




























          up vote
          4
          down vote













          StringUtils.isBlank(foo) will perform a null check for you. If you perform foo.isEmpty() and foo is null, you will raise a NullPointerException.






          share|improve this answer





















          • There is a bigger difference than that; see my answer.
            – arshajii
            May 2 '14 at 0:55


















          up vote
          2
          down vote













          StringUtils.isBlank() returns true for blanks(just whitespaces)and for null String as well. Actually it trims the Char sequences and then performs check.



          StringUtils.isEmpty() returns true when there is no charsequence in the String parameter or when String parameter is null. Difference is that isEmpty() returns false if String parameter contains just whiltespaces. It considers whitespaces as a state of being non empty.






          share|improve this answer




























            up vote
            1
            down vote













            public static boolean isEmpty(String ptext) {
            return ptext == null || ptext.trim().length() == 0;
            }

            public static boolean isBlank(String ptext) {
            return ptext == null || ptext.trim().length() == 0;
            }


            Both have the same code how will isBlank handle white spaces probably you meant isBlankString this has the code for handling whitespaces.



            public static boolean isBlankString( String pString ) {
            int strLength;
            if( pString == null || (strLength = pString.length()) == 0)
            return true;
            for(int i=0; i < strLength; i++)
            if(!Character.isWhitespace(pString.charAt(i)))
            return false;
            return false;
            }





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23419087%2fstringutils-isblank-vs-string-isempty%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              8 Answers
              8






              active

              oldest

              votes








              8 Answers
              8






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              217
              down vote



              accepted










              StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.



              From the linked documentation:




              Checks if a String is whitespace, empty ("") or null.



               StringUtils.isBlank(null)      = true
              StringUtils.isBlank("")        = true
              StringUtils.isBlank(" ")       = true
              StringUtils.isBlank("bob")     = false
              StringUtils.isBlank("  bob  ") = false






              share|improve this answer



























                up vote
                217
                down vote



                accepted










                StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.



                From the linked documentation:




                Checks if a String is whitespace, empty ("") or null.



                 StringUtils.isBlank(null)      = true
                StringUtils.isBlank("")        = true
                StringUtils.isBlank(" ")       = true
                StringUtils.isBlank("bob")     = false
                StringUtils.isBlank("  bob  ") = false






                share|improve this answer

























                  up vote
                  217
                  down vote



                  accepted







                  up vote
                  217
                  down vote



                  accepted






                  StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.



                  From the linked documentation:




                  Checks if a String is whitespace, empty ("") or null.



                   StringUtils.isBlank(null)      = true
                  StringUtils.isBlank("")        = true
                  StringUtils.isBlank(" ")       = true
                  StringUtils.isBlank("bob")     = false
                  StringUtils.isBlank("  bob  ") = false






                  share|improve this answer














                  StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.



                  From the linked documentation:




                  Checks if a String is whitespace, empty ("") or null.



                   StringUtils.isBlank(null)      = true
                  StringUtils.isBlank("")        = true
                  StringUtils.isBlank(" ")       = true
                  StringUtils.isBlank("bob")     = false
                  StringUtils.isBlank("  bob  ") = false







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 2 '14 at 1:01

























                  answered May 2 '14 at 0:54









                  arshajii

                  99.3k18178249




                  99.3k18178249
























                      up vote
                      103
                      down vote













                      The accepted answer from @arshajii is totally correct. However just being more explicit by saying below,



                      StringUtils.isBlank()



                       StringUtils.isBlank(null)      = true
                      StringUtils.isBlank("") = true
                      StringUtils.isBlank(" ") = true
                      StringUtils.isBlank("bob") = false
                      StringUtils.isBlank(" bob ") = false


                      StringUtils.isEmpty



                       StringUtils.isEmpty(null)      = true
                      StringUtils.isEmpty("") = true
                      StringUtils.isEmpty(" ") = false
                      StringUtils.isEmpty("bob") = false
                      StringUtils.isEmpty(" bob ") = false





                      share|improve this answer



























                        up vote
                        103
                        down vote













                        The accepted answer from @arshajii is totally correct. However just being more explicit by saying below,



                        StringUtils.isBlank()



                         StringUtils.isBlank(null)      = true
                        StringUtils.isBlank("") = true
                        StringUtils.isBlank(" ") = true
                        StringUtils.isBlank("bob") = false
                        StringUtils.isBlank(" bob ") = false


                        StringUtils.isEmpty



                         StringUtils.isEmpty(null)      = true
                        StringUtils.isEmpty("") = true
                        StringUtils.isEmpty(" ") = false
                        StringUtils.isEmpty("bob") = false
                        StringUtils.isEmpty(" bob ") = false





                        share|improve this answer

























                          up vote
                          103
                          down vote










                          up vote
                          103
                          down vote









                          The accepted answer from @arshajii is totally correct. However just being more explicit by saying below,



                          StringUtils.isBlank()



                           StringUtils.isBlank(null)      = true
                          StringUtils.isBlank("") = true
                          StringUtils.isBlank(" ") = true
                          StringUtils.isBlank("bob") = false
                          StringUtils.isBlank(" bob ") = false


                          StringUtils.isEmpty



                           StringUtils.isEmpty(null)      = true
                          StringUtils.isEmpty("") = true
                          StringUtils.isEmpty(" ") = false
                          StringUtils.isEmpty("bob") = false
                          StringUtils.isEmpty(" bob ") = false





                          share|improve this answer














                          The accepted answer from @arshajii is totally correct. However just being more explicit by saying below,



                          StringUtils.isBlank()



                           StringUtils.isBlank(null)      = true
                          StringUtils.isBlank("") = true
                          StringUtils.isBlank(" ") = true
                          StringUtils.isBlank("bob") = false
                          StringUtils.isBlank(" bob ") = false


                          StringUtils.isEmpty



                           StringUtils.isEmpty(null)      = true
                          StringUtils.isEmpty("") = true
                          StringUtils.isEmpty(" ") = false
                          StringUtils.isEmpty("bob") = false
                          StringUtils.isEmpty(" bob ") = false






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Oct 10 '17 at 16:50

























                          answered Mar 31 '17 at 21:42









                          nilesh

                          10.7k44564




                          10.7k44564






















                              up vote
                              32
                              down vote













                              StringUtils isEmpty = String isEmpty checks + checks for null.



                              StringUtils isBlank = StringUtils isEmpty checks + checks if the text contains only whitespace character(s).



                              Useful links for further investigation:




                              • StringUtils isBlank documentation

                              • StringUtils isEmpty documentation

                              • String isEmpty documentation






                              share|improve this answer



























                                up vote
                                32
                                down vote













                                StringUtils isEmpty = String isEmpty checks + checks for null.



                                StringUtils isBlank = StringUtils isEmpty checks + checks if the text contains only whitespace character(s).



                                Useful links for further investigation:




                                • StringUtils isBlank documentation

                                • StringUtils isEmpty documentation

                                • String isEmpty documentation






                                share|improve this answer

























                                  up vote
                                  32
                                  down vote










                                  up vote
                                  32
                                  down vote









                                  StringUtils isEmpty = String isEmpty checks + checks for null.



                                  StringUtils isBlank = StringUtils isEmpty checks + checks if the text contains only whitespace character(s).



                                  Useful links for further investigation:




                                  • StringUtils isBlank documentation

                                  • StringUtils isEmpty documentation

                                  • String isEmpty documentation






                                  share|improve this answer














                                  StringUtils isEmpty = String isEmpty checks + checks for null.



                                  StringUtils isBlank = StringUtils isEmpty checks + checks if the text contains only whitespace character(s).



                                  Useful links for further investigation:




                                  • StringUtils isBlank documentation

                                  • StringUtils isEmpty documentation

                                  • String isEmpty documentation







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 10 at 18:09









                                  ChrisOdney

                                  1,69282338




                                  1,69282338










                                  answered Sep 26 '14 at 13:13









                                  yallam

                                  6161712




                                  6161712






















                                      up vote
                                      13
                                      down vote













                                      StringUtils.isBlank() will also check for null, whereas this:



                                      String foo = getvalue("foo");
                                      if (foo.isEmpty())


                                      will throw a NullPointerException if foo is null.






                                      share|improve this answer



















                                      • 4




                                        There is a bigger difference than that; see my answer.
                                        – arshajii
                                        May 2 '14 at 0:56






                                      • 1




                                        This is incorrect. String.isEmpty() will return true if it is null. At least if you are talking about the apache.commons.lang version. I'm not sure about the Spring version.
                                        – ryoung
                                        May 26 '15 at 13:52








                                      • 1




                                        The meaning of my comment was edited away (to be fair it could have been more clear from the get-go); I was not comparing StringUtils.isBlank() to StringUtils.isEmpty() - rather StringUtils.isBlank() to the OP's value.isEmpty()
                                        – chut
                                        May 27 '15 at 17:15










                                      • chut's answer is correct. If java String foo is null, then foo.isEmpty() throws NullPointerException. The apache StringUtils.isBlank(foo) returns true even if foo is null.
                                        – user2590805
                                        Oct 5 at 8:53















                                      up vote
                                      13
                                      down vote













                                      StringUtils.isBlank() will also check for null, whereas this:



                                      String foo = getvalue("foo");
                                      if (foo.isEmpty())


                                      will throw a NullPointerException if foo is null.






                                      share|improve this answer



















                                      • 4




                                        There is a bigger difference than that; see my answer.
                                        – arshajii
                                        May 2 '14 at 0:56






                                      • 1




                                        This is incorrect. String.isEmpty() will return true if it is null. At least if you are talking about the apache.commons.lang version. I'm not sure about the Spring version.
                                        – ryoung
                                        May 26 '15 at 13:52








                                      • 1




                                        The meaning of my comment was edited away (to be fair it could have been more clear from the get-go); I was not comparing StringUtils.isBlank() to StringUtils.isEmpty() - rather StringUtils.isBlank() to the OP's value.isEmpty()
                                        – chut
                                        May 27 '15 at 17:15










                                      • chut's answer is correct. If java String foo is null, then foo.isEmpty() throws NullPointerException. The apache StringUtils.isBlank(foo) returns true even if foo is null.
                                        – user2590805
                                        Oct 5 at 8:53













                                      up vote
                                      13
                                      down vote










                                      up vote
                                      13
                                      down vote









                                      StringUtils.isBlank() will also check for null, whereas this:



                                      String foo = getvalue("foo");
                                      if (foo.isEmpty())


                                      will throw a NullPointerException if foo is null.






                                      share|improve this answer














                                      StringUtils.isBlank() will also check for null, whereas this:



                                      String foo = getvalue("foo");
                                      if (foo.isEmpty())


                                      will throw a NullPointerException if foo is null.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited May 27 '15 at 17:15

























                                      answered May 2 '14 at 0:53









                                      chut

                                      54725




                                      54725








                                      • 4




                                        There is a bigger difference than that; see my answer.
                                        – arshajii
                                        May 2 '14 at 0:56






                                      • 1




                                        This is incorrect. String.isEmpty() will return true if it is null. At least if you are talking about the apache.commons.lang version. I'm not sure about the Spring version.
                                        – ryoung
                                        May 26 '15 at 13:52








                                      • 1




                                        The meaning of my comment was edited away (to be fair it could have been more clear from the get-go); I was not comparing StringUtils.isBlank() to StringUtils.isEmpty() - rather StringUtils.isBlank() to the OP's value.isEmpty()
                                        – chut
                                        May 27 '15 at 17:15










                                      • chut's answer is correct. If java String foo is null, then foo.isEmpty() throws NullPointerException. The apache StringUtils.isBlank(foo) returns true even if foo is null.
                                        – user2590805
                                        Oct 5 at 8:53














                                      • 4




                                        There is a bigger difference than that; see my answer.
                                        – arshajii
                                        May 2 '14 at 0:56






                                      • 1




                                        This is incorrect. String.isEmpty() will return true if it is null. At least if you are talking about the apache.commons.lang version. I'm not sure about the Spring version.
                                        – ryoung
                                        May 26 '15 at 13:52








                                      • 1




                                        The meaning of my comment was edited away (to be fair it could have been more clear from the get-go); I was not comparing StringUtils.isBlank() to StringUtils.isEmpty() - rather StringUtils.isBlank() to the OP's value.isEmpty()
                                        – chut
                                        May 27 '15 at 17:15










                                      • chut's answer is correct. If java String foo is null, then foo.isEmpty() throws NullPointerException. The apache StringUtils.isBlank(foo) returns true even if foo is null.
                                        – user2590805
                                        Oct 5 at 8:53








                                      4




                                      4




                                      There is a bigger difference than that; see my answer.
                                      – arshajii
                                      May 2 '14 at 0:56




                                      There is a bigger difference than that; see my answer.
                                      – arshajii
                                      May 2 '14 at 0:56




                                      1




                                      1




                                      This is incorrect. String.isEmpty() will return true if it is null. At least if you are talking about the apache.commons.lang version. I'm not sure about the Spring version.
                                      – ryoung
                                      May 26 '15 at 13:52






                                      This is incorrect. String.isEmpty() will return true if it is null. At least if you are talking about the apache.commons.lang version. I'm not sure about the Spring version.
                                      – ryoung
                                      May 26 '15 at 13:52






                                      1




                                      1




                                      The meaning of my comment was edited away (to be fair it could have been more clear from the get-go); I was not comparing StringUtils.isBlank() to StringUtils.isEmpty() - rather StringUtils.isBlank() to the OP's value.isEmpty()
                                      – chut
                                      May 27 '15 at 17:15




                                      The meaning of my comment was edited away (to be fair it could have been more clear from the get-go); I was not comparing StringUtils.isBlank() to StringUtils.isEmpty() - rather StringUtils.isBlank() to the OP's value.isEmpty()
                                      – chut
                                      May 27 '15 at 17:15












                                      chut's answer is correct. If java String foo is null, then foo.isEmpty() throws NullPointerException. The apache StringUtils.isBlank(foo) returns true even if foo is null.
                                      – user2590805
                                      Oct 5 at 8:53




                                      chut's answer is correct. If java String foo is null, then foo.isEmpty() throws NullPointerException. The apache StringUtils.isBlank(foo) returns true even if foo is null.
                                      – user2590805
                                      Oct 5 at 8:53










                                      up vote
                                      6
                                      down vote













                                      StringUtils.isBlank also returns true for just whitespace:




                                      isBlank(String str)



                                      Checks if a String is whitespace, empty ("") or null.







                                      share|improve this answer

























                                        up vote
                                        6
                                        down vote













                                        StringUtils.isBlank also returns true for just whitespace:




                                        isBlank(String str)



                                        Checks if a String is whitespace, empty ("") or null.







                                        share|improve this answer























                                          up vote
                                          6
                                          down vote










                                          up vote
                                          6
                                          down vote









                                          StringUtils.isBlank also returns true for just whitespace:




                                          isBlank(String str)



                                          Checks if a String is whitespace, empty ("") or null.







                                          share|improve this answer












                                          StringUtils.isBlank also returns true for just whitespace:




                                          isBlank(String str)



                                          Checks if a String is whitespace, empty ("") or null.








                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered May 2 '14 at 0:54









                                          Octavia Togami

                                          1,85331932




                                          1,85331932






















                                              up vote
                                              4
                                              down vote













                                              StringUtils.isBlank(foo) will perform a null check for you. If you perform foo.isEmpty() and foo is null, you will raise a NullPointerException.






                                              share|improve this answer





















                                              • There is a bigger difference than that; see my answer.
                                                – arshajii
                                                May 2 '14 at 0:55















                                              up vote
                                              4
                                              down vote













                                              StringUtils.isBlank(foo) will perform a null check for you. If you perform foo.isEmpty() and foo is null, you will raise a NullPointerException.






                                              share|improve this answer





















                                              • There is a bigger difference than that; see my answer.
                                                – arshajii
                                                May 2 '14 at 0:55













                                              up vote
                                              4
                                              down vote










                                              up vote
                                              4
                                              down vote









                                              StringUtils.isBlank(foo) will perform a null check for you. If you perform foo.isEmpty() and foo is null, you will raise a NullPointerException.






                                              share|improve this answer












                                              StringUtils.isBlank(foo) will perform a null check for you. If you perform foo.isEmpty() and foo is null, you will raise a NullPointerException.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered May 2 '14 at 0:53









                                              Default

                                              8,85111936




                                              8,85111936












                                              • There is a bigger difference than that; see my answer.
                                                – arshajii
                                                May 2 '14 at 0:55


















                                              • There is a bigger difference than that; see my answer.
                                                – arshajii
                                                May 2 '14 at 0:55
















                                              There is a bigger difference than that; see my answer.
                                              – arshajii
                                              May 2 '14 at 0:55




                                              There is a bigger difference than that; see my answer.
                                              – arshajii
                                              May 2 '14 at 0:55










                                              up vote
                                              2
                                              down vote













                                              StringUtils.isBlank() returns true for blanks(just whitespaces)and for null String as well. Actually it trims the Char sequences and then performs check.



                                              StringUtils.isEmpty() returns true when there is no charsequence in the String parameter or when String parameter is null. Difference is that isEmpty() returns false if String parameter contains just whiltespaces. It considers whitespaces as a state of being non empty.






                                              share|improve this answer

























                                                up vote
                                                2
                                                down vote













                                                StringUtils.isBlank() returns true for blanks(just whitespaces)and for null String as well. Actually it trims the Char sequences and then performs check.



                                                StringUtils.isEmpty() returns true when there is no charsequence in the String parameter or when String parameter is null. Difference is that isEmpty() returns false if String parameter contains just whiltespaces. It considers whitespaces as a state of being non empty.






                                                share|improve this answer























                                                  up vote
                                                  2
                                                  down vote










                                                  up vote
                                                  2
                                                  down vote









                                                  StringUtils.isBlank() returns true for blanks(just whitespaces)and for null String as well. Actually it trims the Char sequences and then performs check.



                                                  StringUtils.isEmpty() returns true when there is no charsequence in the String parameter or when String parameter is null. Difference is that isEmpty() returns false if String parameter contains just whiltespaces. It considers whitespaces as a state of being non empty.






                                                  share|improve this answer












                                                  StringUtils.isBlank() returns true for blanks(just whitespaces)and for null String as well. Actually it trims the Char sequences and then performs check.



                                                  StringUtils.isEmpty() returns true when there is no charsequence in the String parameter or when String parameter is null. Difference is that isEmpty() returns false if String parameter contains just whiltespaces. It considers whitespaces as a state of being non empty.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Feb 11 '15 at 15:15









                                                  user4555388

                                                  211




                                                  211






















                                                      up vote
                                                      1
                                                      down vote













                                                      public static boolean isEmpty(String ptext) {
                                                      return ptext == null || ptext.trim().length() == 0;
                                                      }

                                                      public static boolean isBlank(String ptext) {
                                                      return ptext == null || ptext.trim().length() == 0;
                                                      }


                                                      Both have the same code how will isBlank handle white spaces probably you meant isBlankString this has the code for handling whitespaces.



                                                      public static boolean isBlankString( String pString ) {
                                                      int strLength;
                                                      if( pString == null || (strLength = pString.length()) == 0)
                                                      return true;
                                                      for(int i=0; i < strLength; i++)
                                                      if(!Character.isWhitespace(pString.charAt(i)))
                                                      return false;
                                                      return false;
                                                      }





                                                      share|improve this answer

























                                                        up vote
                                                        1
                                                        down vote













                                                        public static boolean isEmpty(String ptext) {
                                                        return ptext == null || ptext.trim().length() == 0;
                                                        }

                                                        public static boolean isBlank(String ptext) {
                                                        return ptext == null || ptext.trim().length() == 0;
                                                        }


                                                        Both have the same code how will isBlank handle white spaces probably you meant isBlankString this has the code for handling whitespaces.



                                                        public static boolean isBlankString( String pString ) {
                                                        int strLength;
                                                        if( pString == null || (strLength = pString.length()) == 0)
                                                        return true;
                                                        for(int i=0; i < strLength; i++)
                                                        if(!Character.isWhitespace(pString.charAt(i)))
                                                        return false;
                                                        return false;
                                                        }





                                                        share|improve this answer























                                                          up vote
                                                          1
                                                          down vote










                                                          up vote
                                                          1
                                                          down vote









                                                          public static boolean isEmpty(String ptext) {
                                                          return ptext == null || ptext.trim().length() == 0;
                                                          }

                                                          public static boolean isBlank(String ptext) {
                                                          return ptext == null || ptext.trim().length() == 0;
                                                          }


                                                          Both have the same code how will isBlank handle white spaces probably you meant isBlankString this has the code for handling whitespaces.



                                                          public static boolean isBlankString( String pString ) {
                                                          int strLength;
                                                          if( pString == null || (strLength = pString.length()) == 0)
                                                          return true;
                                                          for(int i=0; i < strLength; i++)
                                                          if(!Character.isWhitespace(pString.charAt(i)))
                                                          return false;
                                                          return false;
                                                          }





                                                          share|improve this answer












                                                          public static boolean isEmpty(String ptext) {
                                                          return ptext == null || ptext.trim().length() == 0;
                                                          }

                                                          public static boolean isBlank(String ptext) {
                                                          return ptext == null || ptext.trim().length() == 0;
                                                          }


                                                          Both have the same code how will isBlank handle white spaces probably you meant isBlankString this has the code for handling whitespaces.



                                                          public static boolean isBlankString( String pString ) {
                                                          int strLength;
                                                          if( pString == null || (strLength = pString.length()) == 0)
                                                          return true;
                                                          for(int i=0; i < strLength; i++)
                                                          if(!Character.isWhitespace(pString.charAt(i)))
                                                          return false;
                                                          return false;
                                                          }






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Apr 13 '16 at 7:47









                                                          Apoorva Dhanvanthri

                                                          191




                                                          191






























                                                               

                                                              draft saved


                                                              draft discarded



















































                                                               


                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23419087%2fstringutils-isblank-vs-string-isempty%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              Florida Star v. B. J. F.

                                                              Error while running script in elastic search , gateway timeout

                                                              Adding quotations to stringified JSON object values