and adding an overloaded method for uncapitalize to maintain symmetry
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137846 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f845e36c54
commit
f90d753d06
|
@ -28,7 +28,7 @@ package org.apache.commons.lang;
|
|||
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
|
||||
* @author Gary Gregory
|
||||
* @since 2.0
|
||||
* @version $Id: WordUtils.java,v 1.11 2004/06/03 03:40:28 bayard Exp $
|
||||
* @version $Id: WordUtils.java,v 1.12 2004/06/03 03:49:47 bayard Exp $
|
||||
*/
|
||||
public class WordUtils {
|
||||
|
||||
|
@ -369,20 +369,43 @@ public class WordUtils {
|
|||
* @see #capitalize(String)
|
||||
*/
|
||||
public static String uncapitalize(String str) {
|
||||
int strLen;
|
||||
if (str == null || (strLen = str.length()) == 0) {
|
||||
return uncapitalize(str, null);
|
||||
}
|
||||
|
||||
public static String uncapitalize(String str, char[] delimiters) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return str;
|
||||
}
|
||||
int strLen = str.length();
|
||||
|
||||
int delimitersLen = 0;
|
||||
if(delimiters != null) {
|
||||
delimitersLen = delimiters.length;
|
||||
}
|
||||
|
||||
StringBuffer buffer = new StringBuffer(strLen);
|
||||
boolean whitespace = true;
|
||||
boolean uncapitalizeNext = true;
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
char ch = str.charAt(i);
|
||||
if (Character.isWhitespace(ch)) {
|
||||
|
||||
boolean isDelimiter = false;
|
||||
if(delimiters == null) {
|
||||
isDelimiter = Character.isWhitespace(ch);
|
||||
} else {
|
||||
for(int j=0; j < delimitersLen; j++) {
|
||||
if(ch == delimiters[j]) {
|
||||
isDelimiter = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isDelimiter) {
|
||||
buffer.append(ch);
|
||||
whitespace = true;
|
||||
} else if (whitespace) {
|
||||
uncapitalizeNext = true;
|
||||
} else if (uncapitalizeNext) {
|
||||
buffer.append(Character.toLowerCase(ch));
|
||||
whitespace = false;
|
||||
uncapitalizeNext = false;
|
||||
} else {
|
||||
buffer.append(ch);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ import junit.framework.TestSuite;
|
|||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @author Henri Yandell
|
||||
* @author Stephen Colebourne
|
||||
* @version $Id: WordUtilsTest.java,v 1.6 2004/06/03 03:40:28 bayard Exp $
|
||||
* @version $Id: WordUtilsTest.java,v 1.7 2004/06/03 03:49:47 bayard Exp $
|
||||
*/
|
||||
public class WordUtilsTest extends TestCase {
|
||||
|
||||
|
@ -226,6 +226,20 @@ public class WordUtilsTest extends TestCase {
|
|||
assertEquals("i aM hERE 123", WordUtils.uncapitalize("I AM HERE 123") );
|
||||
}
|
||||
|
||||
public void testUncapitalizeWithDelimiters_String() {
|
||||
assertEquals(null, WordUtils.uncapitalize(null, null));
|
||||
assertEquals("", WordUtils.uncapitalize("", new char[0]));
|
||||
assertEquals(" ", WordUtils.uncapitalize(" ", new char[0]));
|
||||
|
||||
char[] chars = new char[] { '-', '+', ' ', '@' };
|
||||
assertEquals("i", WordUtils.uncapitalize("I", chars) );
|
||||
assertEquals("i", WordUtils.uncapitalize("i", chars) );
|
||||
assertEquals("i am-here+123", WordUtils.uncapitalize("i am-here+123", chars) );
|
||||
assertEquals("i+am here-123", WordUtils.uncapitalize("I+Am Here-123", chars) );
|
||||
assertEquals("i-am+hERE 123", WordUtils.uncapitalize("i-am+HERE 123", chars) );
|
||||
assertEquals("i aM-hERE+123", WordUtils.uncapitalize("I AM-HERE+123", chars) );
|
||||
}
|
||||
|
||||
public void testSwapCase_String() {
|
||||
assertEquals(null, WordUtils.swapCase(null));
|
||||
assertEquals("", WordUtils.swapCase(""));
|
||||
|
|
Loading…
Reference in New Issue