[Schmitzm-commits] r2188 - trunk/schmitzm-core/src/main/java/de/schmitzm/lang

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Jan 11 00:17:32 CET 2013


Author: mojays
Date: 2013-01-11 00:17:32 +0100 (Fri, 11 Jan 2013)
New Revision: 2188

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
LangUtil: new method toCamelCase(.)


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-01-10 18:33:09 UTC (rev 2187)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-01-10 23:17:32 UTC (rev 2188)
@@ -2712,4 +2712,33 @@
     public static <T> void sortList(List<T> sourceList, Comparator<T> comparator) {
       sortList(sourceList,comparator,null);
     }
+    
+    /**
+     * Converts a string to camel-case, so that each word-starting letter 
+     * is in upper-case and any other latters in lower-case.
+     * @param str string to convert
+     * @param inverted indicates whether camel-case is reverted (word-starting letter in
+     *                 upper-case, other letters in lower-case.
+     */
+    public static String toCamelCase(String str, boolean inverted) {
+      Matcher m = Pattern.compile("(^|\\s+)(\\w)").matcher(str);
+      StringBuilder sb = new StringBuilder();
+      int last = 0;
+      while (m.find()) {
+          String rest = str.substring(last, m.start());
+          rest = inverted ? rest.toUpperCase() : rest.toLowerCase();
+          sb.append(rest); // rest upto first/next occurence
+          sb.append(m.group(1)); // white spaces
+          String upperCase = m.group(2); // part to convert in camel
+          upperCase = inverted ? upperCase.toLowerCase() : upperCase.toUpperCase();
+          sb.append(upperCase);
+          last = m.end();
+      }
+      // rest upto end of string
+      String rest = str.substring(last);
+      rest = inverted ? rest.toUpperCase() : rest.toLowerCase();
+      sb.append(rest);
+      
+      return sb.toString();
+    }
 }



More information about the Schmitzm-commits mailing list