commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grzegorz Błaszczyk (JIRA) <j...@apache.org>
Subject [jira] Created: (LANG-415) Two new static methods in StringUtils: camelCaseToUnderscoreSeparated(String) and underscoreSeparatedToCamelCase(String)
Date Mon, 03 Mar 2008 22:38:55 GMT
Two new static methods in StringUtils: camelCaseToUnderscoreSeparated(String) and underscoreSeparatedToCamelCase(String)
------------------------------------------------------------------------------------------------------------------------

                 Key: LANG-415
                 URL: https://issues.apache.org/jira/browse/LANG-415
             Project: Commons Lang
          Issue Type: New Feature
    Affects Versions: 2.3
            Reporter: Grzegorz Błaszczyk
             Fix For: 2.4


Index: /CommonsLang/src/java/org/apache/commons/lang/StringUtils.java
===================================================================
--- /CommonsLang/src/java/org/apache/commons/lang/StringUtils.java	(revision 633306)
+++ /CommonsLang/src/java/org/apache/commons/lang/StringUtils.java	(working copy)
@@ -2782,6 +2782,60 @@
         list.add(new String(c, tokenStart, c.length - tokenStart));
         return (String[]) list.toArray(new String[list.size()]);
     }
+    /**
+     * Changes a camelCase string value to underscore separated
+     * @param input
+     * @param toLowerCase - if output string should be lower case
+     * @return underscore separated string
+     */
+    public static String camelCaseToUnderscoreSeparated(String input,
+			boolean toLowerCase) {
+		StringBuilder s = new StringBuilder();
+		if (input == null) {
+			return "";
+		}
+		int length = input.length();
+		for (int i = 0; i < length; i++) {
+			char ch = input.charAt(i);
+			if (Character.isUpperCase(ch) && i > 0) {
+				s.append("_");
+			}
+			if (ch == '.') {
+				s.append("_");
+			} else {
+				s.append(toLowerCase ? Character.toLowerCase(ch) : Character
+						.toUpperCase(ch));
+			}
+		}
+		return s.toString();
+	}
+	
+    /**
+     * Changes a underscore separated string value to camelCase
+     * @param input
+     * @return camelScape string
+     */
+	public static String underscoreSeparatedToCamelCase(String input) {
+		StringBuilder s = new StringBuilder();
+		if (input == null) {
+			return "";
+		}
+		int length = input.length();
+		boolean upperCase = false;
+		
+		for (int i = 0; i < length; i++) {
+			char ch = input.charAt(i);
+			if (ch == '_') {
+				upperCase = true;
+			} else if (upperCase) {
+				s.append(Character.toUpperCase(ch));
+				upperCase = false;
+			} else {
+				s.append(ch);
+			}
+		}
+		return s.toString();
+	}
 
     // Joining
     //-----------------------------------------------------------------------


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message