You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary Gregory (JIRA)" <ji...@apache.org> on 2008/03/03 23:44:51 UTC
[jira] Commented: (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=12574770#action_12574770 ]
Gary Gregory commented on LANG-415:
-----------------------------------
Requests like this keep on coming in from time to time. It feels to me like we need a new Commons Words project or somesuch. I do not see what this has to do with extensions to java.lang. Am I missing something?
> 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
>
> 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.