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
This commit is contained in:
parent
eb79f7c6f3
commit
9e0f645e8e
|
@ -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.
|
||||
|
|
|
@ -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 <code>null</code>)
|
||||
* @return <code>true</code> 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
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue