Java String Camel Case Format toCamelCaseIgnoringLastChar(String string, String delimiter, boolean upperFirst)

Here you can find the source of toCamelCaseIgnoringLastChar(String string, String delimiter, boolean upperFirst)

Description

to Camel Case Ignoring Last Char

License

Apache License

Declaration

public static String toCamelCaseIgnoringLastChar(String string, String delimiter, boolean upperFirst) 

Method Source Code

//package com.java2s;
/*//  ww w.  j  a  v a2 s  . c o m
Copyright 2016 Goldman Sachs.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
  http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
*/

public class Main {
    public static String toCamelCaseIgnoringLastChar(String string, String delimiter, boolean upperFirst) {
        String trimmed = string.trim();
        String[] delimited;

        if (delimiter.length() > 0) {
            delimited = trimmed.split(delimiter);
        } else {
            delimited = new String[1];
            delimited[0] = string;
        }

        String result = "";

        for (int i = 0; i < delimited.length; i++) {
            /* Break up the string for camelcasing */

            delimited[i] = toCamelCasePresevingExisting(delimited[i]);

            if ((i == 0) & !upperFirst) {
                delimited[i] = firstLetterToLower(delimited[i]);
            } else {
                delimited[i] = firstLetterToUpper(delimited[i]);
            }

            if ((i == delimited.length - 1) && (delimited[i].length() == 1)) {
                break;
            }

            result += delimited[i];
        }

        return result;
    }

    public static String toCamelCasePresevingExisting(String s) {
        char[] result = new char[s.length()];
        for (int i = 0; i < s.length(); i++) {
            boolean uppercaseAllowed = (i == 0) || (Character.isLowerCase(s.charAt(i - 1)));
            result[i] = s.charAt(i);
            if (!uppercaseAllowed) {
                result[i] = Character.toLowerCase(result[i]);
            }
        }
        return new String(result);
    }

    public static String firstLetterToLower(String tmp) {
        return tmp.substring(0, 1).toLowerCase() + tmp.substring(1);
    }

    public static String firstLetterToUpper(String tmp) {
        return tmp.substring(0, 1).toUpperCase() + tmp.substring(1);
    }
}

Related

  1. toCamelCase(String value)
  2. toCamelCase(String value)
  3. toCamelCase(String value, String separator)
  4. toCamelCaseCapitalize(String underLineName)
  5. toCamelCaseIdentifier(String formStr)
  6. toCamelCasePresevingExisting(String s)
  7. toCamelCaseSimple(String str)
  8. toCamelCaseVar(String s)