Adding StringUtil.isBlank(String) and StringUtil.isNotBlank(String) with

tests
This commit is contained in:
Joakim Erdfelt 2012-06-26 10:45:33 -07:00
parent bf0c45d790
commit c5b7ff0fb4
2 changed files with 109 additions and 0 deletions

View File

@ -370,6 +370,85 @@ public class StringUtil
}
}
/* ------------------------------------------------------------ */
/**
* Test if a string is null or only has whitespace characters in it.
* <p>
* Note: uses codepoint version of {@link Character#isWhitespace(int)} to support Unicode better.
*
* <pre>
* isBlank(null) == true
* isBlank("") == true
* isBlank("\r\n") == true
* isBlank("\t") == true
* isBlank(" ") == true
* isBlank("a") == false
* isBlank(".") == false
* isBlank(";\n") == false
* </pre>
*
* @param str
* the string to test.
* @return true if string is null or only whitespace characters, false if non-whitespace characters encountered.
*/
public static boolean isBlank(String str)
{
if (str == null)
{
return true;
}
int len = str.length();
for (int i = 0; i < len; i++)
{
if (!Character.isWhitespace(str.codePointAt(i)))
{
// found a non-whitespace, we can stop searching now
return false;
}
}
// only whitespace
return true;
}
/* ------------------------------------------------------------ */
/**
* Test if a string is not null and contains at least 1 non-whitespace characters in it.
* <p>
* Note: uses codepoint version of {@link Character#isWhitespace(int)} to support Unicode better.
*
* <pre>
* isNotBlank(null) == false
* isNotBlank("") == false
* isNotBlank("\r\n") == false
* isNotBlank("\t") == false
* isNotBlank(" ") == false
* isNotBlank("a") == true
* isNotBlank(".") == true
* isNotBlank(";\n") == true
* </pre>
*
* @param str
* the string to test.
* @return true if string is not null and has at least 1 non-whitespace character, false if null or all-whitespace characters.
*/
public static boolean isNotBlank(String str)
{
if (str == null)
{
return false;
}
int len = str.length();
for (int i = 0; i < len; i++)
{
if (!Character.isWhitespace(str.codePointAt(i)))
{
// found a non-whitespace, we can stop searching now
return true;
}
}
// only whitespace
return false;
}
/* ------------------------------------------------------------ */
public static boolean isUTF8(String charset)

View File

@ -197,4 +197,34 @@ public class StringUtilTest
}
System.err.println(calc);
}
@Test
public void testIsBlank() {
Assert.assertTrue(StringUtil.isBlank(null));
Assert.assertTrue(StringUtil.isBlank(""));
Assert.assertTrue(StringUtil.isBlank("\r\n"));
Assert.assertTrue(StringUtil.isBlank("\t"));
Assert.assertTrue(StringUtil.isBlank(" "));
Assert.assertFalse(StringUtil.isBlank("a"));
Assert.assertFalse(StringUtil.isBlank(" a"));
Assert.assertFalse(StringUtil.isBlank("a "));
Assert.assertFalse(StringUtil.isBlank("."));
Assert.assertFalse(StringUtil.isBlank(";\n"));
}
@Test
public void testIsNotBlank() {
Assert.assertFalse(StringUtil.isNotBlank(null));
Assert.assertFalse(StringUtil.isNotBlank(""));
Assert.assertFalse(StringUtil.isNotBlank("\r\n"));
Assert.assertFalse(StringUtil.isNotBlank("\t"));
Assert.assertFalse(StringUtil.isNotBlank(" "));
Assert.assertTrue(StringUtil.isNotBlank("a"));
Assert.assertTrue(StringUtil.isNotBlank(" a"));
Assert.assertTrue(StringUtil.isNotBlank("a "));
Assert.assertTrue(StringUtil.isNotBlank("."));
Assert.assertTrue(StringUtil.isNotBlank(";\n"));
}
}