diff --git a/src/java/org/apache/commons/lang/WordUtils.java b/src/java/org/apache/commons/lang/WordUtils.java index b79bd0b83..05cf21eca 100644 --- a/src/java/org/apache/commons/lang/WordUtils.java +++ b/src/java/org/apache/commons/lang/WordUtils.java @@ -273,34 +273,17 @@ public class WordUtils { * @since 2.1 */ public static String capitalize(String str, char[] delimiters) { - if (str == null || str.length() == 0) { + int delimLen = (delimiters == null ? -1 : delimiters.length); + if (str == null || str.length() == 0 || delimLen == 0) { return str; } int strLen = str.length(); StringBuffer buffer = new StringBuffer(strLen); - - int delimitersLen = 0; - if(delimiters != null) { - delimitersLen = delimiters.length; - } - boolean capitalizeNext = true; for (int i = 0; i < strLen; i++) { char ch = str.charAt(i); - 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) { + if (isDelimiter(ch, delimiters)) { buffer.append(ch); capitalizeNext = true; } else if (capitalizeNext) { @@ -313,6 +296,7 @@ public class WordUtils { return buffer.toString(); } + //----------------------------------------------------------------------- /** *
Converts all the whitespace separated words in a String into capitalized words, * that is each word is made up of a titlecase character and then a series of @@ -363,13 +347,15 @@ public class WordUtils { * @since 2.1 */ public static String capitalizeFully(String str, char[] delimiters) { - if (str == null || str.length() == 0) { + int delimLen = (delimiters == null ? -1 : delimiters.length); + if (str == null || str.length() == 0 || delimLen == 0) { return str; } str = str.toLowerCase(); return capitalize(str, delimiters); } + //----------------------------------------------------------------------- /** *
Uncapitalizes all the whitespace separated words in a String. * Only the first letter of each word is changed.
@@ -417,34 +403,17 @@ public class WordUtils { * @since 2.1 */ public static String uncapitalize(String str, char[] delimiters) { - if (str == null || str.length() == 0) { + int delimLen = (delimiters == null ? -1 : delimiters.length); + if (str == null || str.length() == 0 || delimLen == 0) { return str; } int strLen = str.length(); - - int delimitersLen = 0; - if(delimiters != null) { - delimitersLen = delimiters.length; - } - StringBuffer buffer = new StringBuffer(strLen); boolean uncapitalizeNext = true; for (int i = 0; i < strLen; i++) { char ch = str.charAt(i); - 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) { + if (isDelimiter(ch, delimiters)) { buffer.append(ch); uncapitalizeNext = true; } else if (uncapitalizeNext) { @@ -457,6 +426,7 @@ public class WordUtils { return buffer.toString(); } + //----------------------------------------------------------------------- /** *Swaps the case of a String using a word based algorithm.
* @@ -510,5 +480,105 @@ public class WordUtils { } return buffer.toString(); } - + + //----------------------------------------------------------------------- + /** + *Extracts the initial letters from each word in the String.
+ * + *The first letter of the string and all first letters after + * whitespace are returned as a new string. + * Their case is not changed.
+ * + *Whitespace is defined by {@link Character#isWhitespace(char)}.
+ * A null
input String returns null
.
+ * WordUtils.initials(null) = null + * WordUtils.initials("") = "" + * WordUtils.initials("Ben John Lee") = "BJL" + * WordUtils.initials("Ben J.Lee") = "BJ" + *+ * + * @param str the String to get initials from, may be null + * @return String of initial letters,
null
if null String input
+ * @see #initials(String,char[])
+ * @since 2.2
+ */
+ public static String initials(String str) {
+ return initials(str, null);
+ }
+
+ /**
+ * Extracts the initial letters from each word in the String.
+ * + *The first letter of the string and all first letters after the + * defined delimiters are returned as a new string. + * Their case is not changed.
+ * + *If the delimiters array is null, then Whitespace is used.
+ * Whitespace is defined by {@link Character#isWhitespace(char)}.
+ * A null
input String returns null
.
+ * An empty delimiter array returns an empty String.
+ * WordUtils.initials(null, *) = null + * WordUtils.initials("", *) = "" + * WordUtils.initials("Ben John Lee", null) = "BJL" + * WordUtils.initials("Ben J.Lee", null) = "BJ" + * WordUtils.initials("Ben J.Lee", [' ','.']) = "BJL" + * WordUtils.initials(*, new char[0]) = "" + *+ * + * @param str the String to get initials from, may be null + * @param delimiters set of characters to determine words, null means whitespace + * @return String of initial letters,
null
if null String input
+ * @see #initials(String)
+ * @since 2.2
+ */
+ public static String initials(String str, char[] delimiters) {
+ if (str == null || str.length() == 0) {
+ return str;
+ }
+ if (delimiters != null && delimiters.length == 0) {
+ return "";
+ }
+ int strLen = str.length();
+ char[] buf = new char[strLen / 2 + 1];
+ int count = 0;
+ boolean lastWasGap = true;
+ for (int i = 0; i < strLen; i++) {
+ char ch = str.charAt(i);
+
+ if (isDelimiter(ch, delimiters)) {
+ lastWasGap = true;
+ } else if (lastWasGap) {
+ buf[count++] = ch;
+ lastWasGap = false;
+ } else {
+ // ignore ch
+ }
+ }
+ return new String(buf, 0, count);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Is the character a delimiter.
+ *
+ * @param ch the character to check
+ * @param delimiters the delimiters
+ * @return true if it is a delimiter
+ */
+ private static boolean isDelimiter(char ch, char[] delimiters) {
+ if (delimiters == null) {
+ return Character.isWhitespace(ch);
+ }
+ for (int i = 0, isize = delimiters.length; i < isize; i++) {
+ if (ch == delimiters[i]) {
+ return true;
+ }
+ }
+ return false;
+ }
+
}
diff --git a/src/test/org/apache/commons/lang/WordUtilsTest.java b/src/test/org/apache/commons/lang/WordUtilsTest.java
index a788252c9..8100637c5 100644
--- a/src/test/org/apache/commons/lang/WordUtilsTest.java
+++ b/src/test/org/apache/commons/lang/WordUtilsTest.java
@@ -248,7 +248,103 @@ public class WordUtilsTest extends TestCase {
assertEquals("i AM.fINE", WordUtils.uncapitalize("I AM.FINE", chars) );
assertEquals("i aM.FINE", WordUtils.uncapitalize("I AM.FINE", null) );
}
-
+
+ //-----------------------------------------------------------------------
+ public void testInitials_String() {
+ assertEquals(null, WordUtils.initials(null));
+ assertEquals("", WordUtils.initials(""));
+ assertEquals("", WordUtils.initials(" "));
+
+ assertEquals("I", WordUtils.initials("I"));
+ assertEquals("i", WordUtils.initials("i"));
+ assertEquals("BJL", WordUtils.initials("Ben John Lee"));
+ assertEquals("BJ", WordUtils.initials("Ben J.Lee"));
+ assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee"));
+ assertEquals("iah1", WordUtils.initials("i am here 123"));
+ }
+
+ // -----------------------------------------------------------------------
+ public void testInitials_String_charArray() {
+ char[] array = null;
+ assertEquals(null, WordUtils.initials(null, array));
+ assertEquals("", WordUtils.initials("", array));
+ assertEquals("", WordUtils.initials(" ", array));
+ assertEquals("I", WordUtils.initials("I", array));
+ assertEquals("i", WordUtils.initials("i", array));
+ assertEquals("S", WordUtils.initials("SJC", array));
+ assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
+ assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
+ assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee", array));
+ assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
+ assertEquals("iah1", WordUtils.initials("i am here 123", array));
+
+ array = new char[0];
+ assertEquals(null, WordUtils.initials(null, array));
+ assertEquals("", WordUtils.initials("", array));
+ assertEquals("", WordUtils.initials(" ", array));
+ assertEquals("", WordUtils.initials("I", array));
+ assertEquals("", WordUtils.initials("i", array));
+ assertEquals("", WordUtils.initials("SJC", array));
+ assertEquals("", WordUtils.initials("Ben John Lee", array));
+ assertEquals("", WordUtils.initials("Ben J.Lee", array));
+ assertEquals("", WordUtils.initials(" Ben John . Lee", array));
+ assertEquals("", WordUtils.initials("Kay O'Murphy", array));
+ assertEquals("", WordUtils.initials("i am here 123", array));
+
+ array = " ".toCharArray();
+ assertEquals(null, WordUtils.initials(null, array));
+ assertEquals("", WordUtils.initials("", array));
+ assertEquals("", WordUtils.initials(" ", array));
+ assertEquals("I", WordUtils.initials("I", array));
+ assertEquals("i", WordUtils.initials("i", array));
+ assertEquals("S", WordUtils.initials("SJC", array));
+ assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
+ assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
+ assertEquals("BJ.L", WordUtils.initials(" Ben John . Lee", array));
+ assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
+ assertEquals("iah1", WordUtils.initials("i am here 123", array));
+
+ array = " .".toCharArray();
+ assertEquals(null, WordUtils.initials(null, array));
+ assertEquals("", WordUtils.initials("", array));
+ assertEquals("", WordUtils.initials(" ", array));
+ assertEquals("I", WordUtils.initials("I", array));
+ assertEquals("i", WordUtils.initials("i", array));
+ assertEquals("S", WordUtils.initials("SJC", array));
+ assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
+ assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
+ assertEquals("BJL", WordUtils.initials(" Ben John . Lee", array));
+ assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
+ assertEquals("iah1", WordUtils.initials("i am here 123", array));
+
+ array = " .'".toCharArray();
+ assertEquals(null, WordUtils.initials(null, array));
+ assertEquals("", WordUtils.initials("", array));
+ assertEquals("", WordUtils.initials(" ", array));
+ assertEquals("I", WordUtils.initials("I", array));
+ assertEquals("i", WordUtils.initials("i", array));
+ assertEquals("S", WordUtils.initials("SJC", array));
+ assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
+ assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
+ assertEquals("BJL", WordUtils.initials(" Ben John . Lee", array));
+ assertEquals("KOM", WordUtils.initials("Kay O'Murphy", array));
+ assertEquals("iah1", WordUtils.initials("i am here 123", array));
+
+ array = "SIJo1".toCharArray();
+ assertEquals(null, WordUtils.initials(null, array));
+ assertEquals("", WordUtils.initials("", array));
+ assertEquals(" ", WordUtils.initials(" ", array));
+ assertEquals("", WordUtils.initials("I", array));
+ assertEquals("i", WordUtils.initials("i", array));
+ assertEquals("C", WordUtils.initials("SJC", array));
+ assertEquals("Bh", WordUtils.initials("Ben John Lee", array));
+ assertEquals("B.", WordUtils.initials("Ben J.Lee", array));
+ assertEquals(" h", WordUtils.initials(" Ben John . Lee", array));
+ assertEquals("K", WordUtils.initials("Kay O'Murphy", array));
+ assertEquals("i2", WordUtils.initials("i am here 123", array));
+ }
+
+ // -----------------------------------------------------------------------
public void testSwapCase_String() {
assertEquals(null, WordUtils.swapCase(null));
assertEquals("", WordUtils.swapCase(""));