[
https://issues.apache.org/jira/browse/LANG-415?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12574797#action_12574797
]
Paul Benedict commented on LANG-415:
------------------------------------
Is it possible to accomplish this using regular expressions? This is definitely
not something for StringUtils, imo. I am not saying it doesn't belong in
Commons or Commons Lang, but perhaps WordUtils may be better suited.
> 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.