You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "James Carman (JIRA)" <ji...@apache.org> on 2008/03/04 00:12:51 UTC
[jira] Issue Comment Edited: (LANG-415) Two new static methods in
StringUtils: camelCaseToUnderscoreSeparated(String) and
underscoreSeparatedToCamelCase(String)
[ https://issues.apache.org/jira/browse/LANG-415?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12574781#action_12574781 ]
jwcarman edited comment on LANG-415 at 3/3/08 3:12 PM:
-----------------------------------------------------------
The fact that this is how you wanted to do property/column mappings in your project doesn't necessarily mean that it's the way everyone would want to do it. As suggested in email, we might want to allow for a separator character (or string) to be passed in. Also, a set of unit tests that exhibit the behavior of the methods would be nice for a patch like this. I'm not saying this method isn't useful (I could use it), I just think that to put it into a library like Commons Lang, we need to make it applicable to more use cases (such as wanting to use '-' as the separator for instance). Also, wouldn't this maybe belong in WordUtils instead, since you're separating a string of concatenated, capitalized words into a string of separated, uncapitalized, delimited words.
was (Author: jwcarman):
The fact that this is how you wanted to do property -> column mappings in your project doesn't necessarily mean that it's the way everyone would want to do it. As suggested in email, we might want to allow for a separator character (or string) to be passed in. Also, a set of unit tests that exhibit the behavior of the methods would be nice for a patch like this. I'm not saying this method isn't useful (I could use it), I just think that to put it into a library like Commons Lang, we need to make it applicable to more use cases (such as wanting to use '-' as the separator for instance). Also, wouldn't this maybe belong in WordUtils instead, since you're separating a string of concatenated, capitalized words into a string of separated, uncapitalized, delimited words.
> 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.