diff --git a/src/java/org/apache/commons/lang/WordUtils.java b/src/java/org/apache/commons/lang/WordUtils.java index 9beee0b3d..b6672ae03 100644 --- a/src/java/org/apache/commons/lang/WordUtils.java +++ b/src/java/org/apache/commons/lang/WordUtils.java @@ -66,7 +66,7 @@ * @author Henning P. Schmiedehausen * @author Gary Gregory * @since 2.0 - * @version $Id: WordUtils.java,v 1.2 2003/08/17 22:56:11 scolebourne Exp $ + * @version $Id: WordUtils.java,v 1.3 2003/08/17 23:06:11 scolebourne Exp $ */ public class WordUtils { @@ -256,7 +256,8 @@ public static String wrap(String str, int wrapLength, String newLineStr, boolean //----------------------------------------------------------------------- /** *

Capitalizes all the whitespace separated words in a String. - * Only the first letter of each word is changed.

+ * Only the first letter of each word is changed. To change all letters to + * the capitalized case, use {@link #capitalizeFully(String)}.

* *

Whitespace is defined by {@link Character#isWhitespace(char)}. * A null input String returns null. @@ -271,6 +272,7 @@ public static String wrap(String str, int wrapLength, String newLineStr, boolean * * @param str the String to capitalize, may be null * @return capitalized String, null if null String input + * @see #capitalizeFully(String) */ public static String capitalize(String str) { int strLen; @@ -294,6 +296,46 @@ public static String capitalize(String str) { return buffer.toString(); } + /** + *

Capitalizes all the whitespace separated words in a String. + * All letters are changed, so the resulting string will be fully changed.

+ * + *

Whitespace is defined by {@link Character#isWhitespace(char)}. + * A null input String returns null. + * Capitalization uses the unicode title case, normally equivalent to + * upper case.

+ * + *
+     * WordUtils.capitalize(null)        = null
+     * WordUtils.capitalize("")          = ""
+     * WordUtils.capitalize("i am FINE") = "I Am Fine"
+     * 
+ * + * @param str the String to capitalize, may be null + * @return capitalized String, null if null String input + */ + public static String capitalizeFully(String str) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return str; + } + StringBuffer buffer = new StringBuffer(strLen); + boolean whitespace = true; + for (int i = 0; i < strLen; i++) { + char ch = str.charAt(i); + if (Character.isWhitespace(ch)) { + buffer.append(ch); + whitespace = true; + } else if (whitespace) { + buffer.append(Character.toTitleCase(ch)); + whitespace = false; + } else { + buffer.append(Character.toLowerCase(ch)); + } + } + return buffer.toString(); + } + /** *

Uncapitalizes all the whitespace separated words in a String. * Only the first letter of each word is changed.

diff --git a/src/test/org/apache/commons/lang/WordUtilsTest.java b/src/test/org/apache/commons/lang/WordUtilsTest.java index a46de4b38..508c917cd 100644 --- a/src/test/org/apache/commons/lang/WordUtilsTest.java +++ b/src/test/org/apache/commons/lang/WordUtilsTest.java @@ -66,7 +66,7 @@ * @author Ringo De Smet * @author Henri Yandell * @author Stephen Colebourne - * @version $Id: WordUtilsTest.java,v 1.2 2003/08/17 22:56:11 scolebourne Exp $ + * @version $Id: WordUtilsTest.java,v 1.3 2003/08/17 23:06:11 scolebourne Exp $ */ public class WordUtilsTest extends TestCase { @@ -210,6 +210,19 @@ public void testCapitalize_String() { assertEquals("I AM HERE 123", WordUtils.capitalize("I AM HERE 123") ); } + public void testCapitalizeFully_String() { + assertEquals(null, WordUtils.capitalizeFully(null)); + assertEquals("", WordUtils.capitalizeFully("")); + assertEquals(" ", WordUtils.capitalizeFully(" ")); + + assertEquals("I", WordUtils.capitalize("I") ); + assertEquals("I", WordUtils.capitalize("i") ); + assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am here 123") ); + assertEquals("I Am Here 123", WordUtils.capitalizeFully("I Am Here 123") ); + assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am HERE 123") ); + assertEquals("I Am Here 123", WordUtils.capitalizeFully("I AM HERE 123") ); + } + public void testUncapitalize_String() { assertEquals(null, WordUtils.uncapitalize(null)); assertEquals("", WordUtils.uncapitalize(""));