mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-10 03:55:10 +00:00
Add capitalizeFully() to deal with a whole string whatever its current state
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137621 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d1d8827200
commit
c9fe5f02aa
@ -66,7 +66,7 @@
|
||||
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
|
||||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||
* @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
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Capitalizes all the whitespace separated words in a String.
|
||||
* Only the first letter of each word is changed.</p>
|
||||
* Only the first letter of each word is changed. To change all letters to
|
||||
* the capitalized case, use {@link #capitalizeFully(String)}.</p>
|
||||
*
|
||||
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
|
||||
* A <code>null</code> input String returns <code>null</code>.
|
||||
@ -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, <code>null</code> 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Capitalizes all the whitespace separated words in a String.
|
||||
* All letters are changed, so the resulting string will be fully changed.</p>
|
||||
*
|
||||
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
|
||||
* A <code>null</code> input String returns <code>null</code>.
|
||||
* Capitalization uses the unicode title case, normally equivalent to
|
||||
* upper case.</p>
|
||||
*
|
||||
* <pre>
|
||||
* WordUtils.capitalize(null) = null
|
||||
* WordUtils.capitalize("") = ""
|
||||
* WordUtils.capitalize("i am FINE") = "I Am Fine"
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to capitalize, may be null
|
||||
* @return capitalized String, <code>null</code> 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Uncapitalizes all the whitespace separated words in a String.
|
||||
* Only the first letter of each word is changed.</p>
|
||||
|
@ -66,7 +66,7 @@
|
||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @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(""));
|
||||
|
Loading…
x
Reference in New Issue
Block a user