the removeUnderScores method doesn't quite do what you might expect from 
the documentation - it not only removes underscores and capitalizes but 
it also un-capitalizes (some) other capitals in the string.

since this isn't really a bug, this patch contains improved doc comments 
for this method (and for firstLetterCaps) plus removeAndHump and 
capitalizeFirstLetter methods that are similar to the above methods but 
which leave the other capitalization alone.

- robert


Index: src/java/org/apache/velocity/util/StringUtils.java
===================================================================
RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/StringUtils.java,v
retrieving revision 1.12
diff -u -r1.12 StringUtils.java
--- src/java/org/apache/velocity/util/StringUtils.java  2001/05/09 01:56:52     1.12
+++ src/java/org/apache/velocity/util/StringUtils.java  2001/06/26 21:29:26
@@ -119,10 +119,13 @@
     }
 
     /**
-     * Remove Underscores from a string and replaces first
-     * Letters with Capitals.  foo_bar becomes FooBar.
+     * <p> Remove underscores from a string and replaces first
+     * letters with capitals.  Other letters are changed to lower case. 
      *
-     * @param String string to remove underscores from.
+     * <p> For example <code>foo_bar</code> becomes <code>FooBar</code>
+     * but <code>foo_barBar</code> becomes <code>FooBarbar</code>.
+     *
+     * @param data string to remove underscores from.
      * @return String 
      */
     static public String removeUnderScores (String data)
@@ -140,16 +143,76 @@
         return out.toString();
     }
 
+    /**
+     * <p> 'Camels Hump' replacement of underscores.
+     *
+     * <p> Remove underscores from a string but leave the capitalization of the
+     * other letters unchanged.
+     *
+     * <p> For example <code>foo_barBar</code> becomes <code>FooBarBar</code>.
+     *
+     * @param data string to hump
+     * @return String 
+     */
+    static public String removeAndHump (String data)
+    {
+       return removeAndHump(data,"_");
+    }
+
+    /**
+     * <p> 'Camels Hump' replacement.
+     *
+     * <p> Remove one string from another string but leave the capitalization of the
+     * other letters unchanged.
+     *
+     * <p> For example, removing "_" from <code>foo_barBar</code> becomes 
+<code>FooBarBar</code>.
+     *
+     * @param data string to hump
+     * @param replaceThis string to be replaced
+     * @return String 
+     */
+    static public String removeAndHump (String data,String replaceThis)
+    {
+        String temp = null;
+        StringBuffer out = new StringBuffer();
+        temp = data;
+
+        StringTokenizer st = new StringTokenizer(temp, replaceThis);
+        while (st.hasMoreTokens())
+        {
+            String element = (String) st.nextElement();
+            out.append ( capitalizeFirstLetter(element));
+        }//while
+        return out.toString();
+    }
+
     /**
-     * Makes the first letter caps and the rest lowercase.
+     * <p> Makes the first letter caps and the rest lowercase.
+     *
+     * <p> For example <code>fooBar</code> becomes <code>Foobar</code>.
      *
-     * @param String
+     * @param data capitalize this
      * @return String
      */
     static public String firstLetterCaps ( String data )
     {
         String firstLetter = data.substring(0,1).toUpperCase();
         String restLetters = data.substring(1).toLowerCase();
+        return firstLetter + restLetters;
+    }
+
+    /**
+     * <p> Capitalize the first letter but leave the rest as they are. 
+     *
+     * <p> For example <code>fooBar</code> becomes <code>FooBar</code>.
+     *
+     * @param data capitalize this
+     * @return String
+     */
+    static public String capitalizeFirstLetter ( String data )
+    {
+        String firstLetter = data.substring(0,1).toUpperCase();
+        String restLetters = data.substring(1);
         return firstLetter + restLetters;
     }
 

Reply via email to