Java String Camel Case To camelCaseToSeparatorCase(String s, String separator)

Here you can find the source of camelCaseToSeparatorCase(String s, String separator)

Description

camel Case To Separator Case

License

LGPL

Declaration

static protected String camelCaseToSeparatorCase(String s, String separator) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    static protected String camelCaseToSeparatorCase(String s, String separator) {

        char[] input = s.toCharArray();

        StringBuilder output = new StringBuilder();

        for (int i = 0; i < input.length; i++) {
            char c = input[i];
            if (Character.isLowerCase(c) || isSpecialCharacter(c)) {
                output.append(c);/*from  www  . j  ava 2 s .c om*/
            } else {
                if (i > 0) {
                    if ((previousCharIsLowerCase(input, i) || nextCharIsLowerCase(input, i))
                            && (!isSpecialCharacter(input[i - 1]) && !isSpecialCharacter(input[i + 1]))) {
                        output.append(separator);
                    }
                }
                output.append(Character.toLowerCase(c));
            }
        }

        String result = output.toString();

        return result;
    }

    static private boolean isSpecialCharacter(char c) {

        return !Character.isLetterOrDigit(c);
    }

    static private boolean previousCharIsLowerCase(char[] input, int i) {

        return Character.isLowerCase(input[i - 1]);
    }

    static private boolean nextCharIsLowerCase(char[] input, int i) {

        return i < (input.length - 1) && Character.isLowerCase(input[i + 1]);
    }
}

Related

  1. camelCaseToEnum(String name)
  2. camelCaseToHuman(String input)
  3. camelCaseToPhrase(String camelCase)
  4. camelCaseToPretty(String camelCase)
  5. camelCaseToSeparatedWords(String ccString, String separator)
  6. camelCaseToSpacedString(String camel)
  7. camelcaseToUppercase(String camelCase)
  8. camelCaseToWords(String data)
  9. camelCaseUnderscores(String str)