commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Matt Benson (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-415) Two new static methods in StringUtils: camelCaseToUnderscoreSeparated(String) and underscoreSeparatedToCamelCase(String)
Date Tue, 04 Mar 2008 15:05:40 GMT

    [ https://issues.apache.org/jira/browse/LANG-415?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12575015#action_12575015
] 

Matt Benson commented on LANG-415:
----------------------------------

As Henri mentioned on user@c.a.o, LANG-192 added StringUtils.splitByCharacterTypeCamelCase(String)
which if used in conjunction with join(String[], char) or join(String[], String) already provides
for this with a IMHO not-unreasonable 2-method/single-line combination:

String propertyName = "fooBarBaz";
String columnName = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(propertyName),
'_');

This feels like a worksforme/wontfix scenario.


> 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
>
>         Attachments: LANG-415.patch
>
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> 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