His way will only be faster if the string is blank. If the string is not blank then the trim function will chew up several processing cycles. An example, say we pass in " dog"...

trim will trim off the first character and check to see if there are more to trim on the beginning and then check the end of the string. The commons method will return false after checking only two characters. make sense?

Bill Dubin wrote:
Below are two implementations for the isBlank() method.  One is from the
Commons Lang and the other was coded by a colleague of mine and he claims
his way is better and faster.
I would like to understand how or why his or the Commons approach is better.
Maybe one is not better, but I still would like to have an understanding of
how and/or why someone would choose one way over the other so I can further
my software development skillset.  Maybe there are ramifications one way or
the other that I'm not seeing and someone can shed some light on for me?

My only motivation for this question is to learn and understand this stuff
better.  Thank you for any explanation you can provide!!  I do appreciate
any assistance or lessons!!

//From Commons project

public static boolean isBlank(String str) {

        int strLen;

        if (str == null || (strLen = str.length()) == 0) {

            return true;

        }

        for (int i = 0; i < strLen; i++) {

            if ((Character.isWhitespace(str.charAt(i)) == false)) {

                return false;

            }

        }

        return true;

Versus this way of doing the test:

//Modified with faster logic according to one of our developers
public static boolean isBlank(String str){

      int strLen;

      if ((str == null) || ((strLen = str.length()) == 0))  {

            return true;

      }

      else  {

      str = str.trim();

      if (str.length() == 0)  {

                  return true;

            }

            else        {

                  return false;

            }

      }

}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to