如何在大海捞针中检查针的空值和空字符串?

pho*_*nix 2 java

情况:我在代码中遇到了很多检查.而且我想知道一种可以减少它们的方法.

if(needle!=null && haystack!=null)
{
  if(needle.length()==0)
   return true;
  else
  {
   if(haystack.length()==0)
   return false;
   else
   {
     // Do 2 for loops to check character by character comparison in a substring  
   }
 }

}
else 
 return false;
Run Code Online (Sandbox Code Playgroud)

Ste*_*ice 5

也许不同的代码样式会增加代码的可读性并减少if所有检查的嵌套语句数量:

if (needle == null || haystack == null || haystack.isEmpty())
    return false;

if (needle.isEmpty())
    return true;

// compare strings here and return result.
Run Code Online (Sandbox Code Playgroud)