diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java
index 421ea8756..160c9b34b 100644
--- a/src/java/org/apache/commons/lang/StringUtils.java
+++ b/src/java/org/apache/commons/lang/StringUtils.java
@@ -4581,6 +4581,18 @@ public class StringUtils {
}
}
+ /**
+ * Gets a String's length or 0
if the String is null
.
+ *
+ * @param str
+ * a String or null
+ * @return String length or 0
if the String is null
.
+ * @since 2.4
+ */
+ public static int length(String str) {
+ return str == null ? 0 : str.length();
+ }
+
// Centering
//-----------------------------------------------------------------------
/**
diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java
index 49021aff5..1bd511149 100644
--- a/src/test/org/apache/commons/lang/StringUtilsTest.java
+++ b/src/test/org/apache/commons/lang/StringUtilsTest.java
@@ -1403,6 +1403,15 @@ public class StringUtilsTest extends TestCase {
assertEquals(" abc", StringUtils.leftPad("abc", 5, ""));
}
+ public void testLength() {
+ assertEquals(0, StringUtils.length(null));
+ assertEquals(0, StringUtils.length(""));
+ assertEquals(0, StringUtils.length(StringUtils.EMPTY));
+ assertEquals(1, StringUtils.length("A"));
+ assertEquals(1, StringUtils.length(" "));
+ assertEquals(8, StringUtils.length("ABCDEFGH"));
+ }
+
//-----------------------------------------------------------------------
public void testCenter_StringInt() {
assertEquals(null, StringUtils.center(null, -1));
@@ -1647,6 +1656,7 @@ public class StringUtilsTest extends TestCase {
public void testEMPTY() {
assertNotNull(StringUtils.EMPTY);
assertEquals("", StringUtils.EMPTY);
+ assertEquals(0, StringUtils.EMPTY.length());
}
public void testRemoveStart() {