Both of the following do the same
Given the following String somestring; void dosomething(){}
Consider the following code
if (somestring!=null&&!"".equals(somestring))
{
//if the string is not empty
dosomething();
}
Create a function as follows
boolean isValid(String somestring)
{
if (somestring == null)
{
//the string is null. so it is not a valid string
return false;
}
//the string is not null
//eliminate empty characters at both ends
String newstring = somestring.trim();
if (newstring.equals(""))
{
//the string is an empty string
//an empty string is (not) valid
return false;
}
//it is a valid string because it is not null, it is not empty
return true;
}
Then you can do
if (isValid(somestring))
{
//if the string is not empty
dosomething();
}
if (somestring!=null&&!"".equals(somestring))
{
//if the string is not empty
dosomething();
}
if (isValid(somestring))
{
//if the string is not empty
dosomething();
}