From 9e0f645e8ee2ac6141d3bf359cf29384cbd30b3c Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Tue, 22 Jun 2010 05:44:47 +0000 Subject: [PATCH] Adding containsWhitespace method per LANG-625. Code comes from the Spring framework, so I've added such to the NOTICE file. License is Apache License 2.0. Unit test is original. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@956775 13f79535-47bb-0310-9956-ffa450edef68 --- NOTICE.txt | 2 ++ .../org/apache/commons/lang3/StringUtils.java | 22 +++++++++++++++++++ .../lang3/StringUtilsEqualsIndexOfTest.java | 10 +++++++++ 3 files changed, 34 insertions(+) diff --git a/NOTICE.txt b/NOTICE.txt index 032e12776..ad434a448 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -3,3 +3,5 @@ Copyright 2001-2010 The Apache Software Foundation This product includes software developed by The Apache Software Foundation (http://www.apache.org/). + +This product includes software from the Spring Framework. diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 67a566e33..a480c131f 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -1347,6 +1347,28 @@ public class StringUtils { return false; } + /** + * Check whether the given String contains any whitespace characters. + * @param str the String to check (may be null) + * @return true if the String is not empty and + * contains at least 1 whitespace character + * @see java.lang.Character#isWhitespace + * @since 3.0 + */ + // From org.springframework.util.StringUtils, under Apache License 2.0 + public static boolean containsWhitespace(String str) { + if (isEmpty(str)) { + return false; + } + int strLen = str.length(); + for (int i = 0; i < strLen; i++) { + if (Character.isWhitespace(str.charAt(i))) { + return true; + } + } + return false; + } + // IndexOfAny chars //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java index 84f5cbaa0..a65749869 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java @@ -431,6 +431,16 @@ public class StringUtilsEqualsIndexOfTest extends TestCase { assertEquals(true, StringUtils.containsOnly(str3, chars3)); } + public void testContainsWhitespace() { + assertFalse( StringUtils.containsWhitespace("") ); + assertTrue( StringUtils.containsWhitespace(" ") ); + assertFalse( StringUtils.containsWhitespace("a") ); + assertTrue( StringUtils.containsWhitespace("a ") ); + assertTrue( StringUtils.containsWhitespace(" a") ); + assertTrue( StringUtils.containsWhitespace("a\t") ); + assertTrue( StringUtils.containsWhitespace("\n") ); + } + public void testEquals() { assertEquals(true, StringUtils.equals(null, null)); assertEquals(true, StringUtils.equals(FOO, FOO));