Java String Camel Case Format toCamelCase(String str, boolean firstCapital)

Here you can find the source of toCamelCase(String str, boolean firstCapital)

Description

to Camel Case

License

MIT License

Declaration

public static String toCamelCase(String str, boolean firstCapital) 

Method Source Code

//package com.java2s;
/*!/*from ww w. ja v  a  2s . com*/
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

public class Main {
    public static String toCamelCase(String str, boolean firstCapital) {
        return changeSeparator(str, -1, true, Boolean.valueOf(firstCapital), null);
    }

    public static String changeSeparator(String str, int separatorChar, boolean capitalize, Boolean firstCapital,
            Boolean upper) {
        if (str == null) {
            return null;
        }

        int len = str.length();

        if (len == 0) {
            return "";
        }

        int cnt = Character.codePointCount(str, 0, len);

        StringBuilder sb = new StringBuilder(len);
        boolean inWord = false;
        boolean isFirstWord = false;
        boolean isFirstLineWord = true;
        boolean isBeforeUpper = false;
        for (int i = 0; i < cnt; i++) {
            int cp = str.codePointAt(i);

            if (isASCII(cp)) {
                if (Character.isAlphabetic(cp) || Character.isDigit(cp)) {
                    if (Character.isUpperCase(cp)) {
                        isFirstWord = !isBeforeUpper;
                        if (!isFirstWord && i + 1 < cnt) {
                            int ncp = str.codePointAt(i + 1);
                            if (Character.isAlphabetic(ncp)
                                    && !(Character.isUpperCase(ncp) || Character.isDigit(cp))) {
                                isFirstWord = true;
                            }
                        }
                        isBeforeUpper = true;
                    } else {
                        isFirstWord = !inWord;
                        isBeforeUpper = false;
                    }
                    inWord = true;
                } else if (cp == '\r' || cp == '\n') {
                    isFirstWord = false;
                    inWord = false;
                    isBeforeUpper = false;

                    isFirstLineWord = true;
                } else {
                    isFirstWord = false;
                    inWord = false;
                    isBeforeUpper = false;
                }
            } else {
                isFirstWord = !inWord;
                inWord = true;
                isBeforeUpper = false;
            }

            if (inWord) {
                if (isFirstWord) {
                    if (isFirstLineWord) {
                        isFirstLineWord = false;
                    } else {
                        if (0 <= separatorChar) {
                            sb.append((char) separatorChar);
                        }
                    }
                }
                if (capitalize) {
                    if (isFirstWord) {
                        if (i == 0) {
                            if (firstCapital == null || firstCapital.booleanValue()) {
                                sb.appendCodePoint(Character.toUpperCase(cp));
                            } else {
                                sb.appendCodePoint(Character.toLowerCase(cp));
                            }
                        } else {
                            sb.appendCodePoint(Character.toUpperCase(cp));
                        }
                    } else {
                        sb.appendCodePoint(Character.toLowerCase(cp));
                    }
                } else {
                    if (upper == null) {
                        sb.appendCodePoint(cp);
                    } else if (upper.booleanValue()) {
                        sb.appendCodePoint(Character.toUpperCase(cp));
                    } else {
                        sb.appendCodePoint(Character.toLowerCase(cp));
                    }
                }
            } else {
                if (cp == '\r' || cp == '\n') {
                    sb.appendCodePoint(cp);
                }
            }
        }

        return sb.toString();
    }

    public static boolean isASCII(char c) {
        if ('\u007F' < c) {
            return false;
        }
        return true;
    }

    public static boolean isASCII(int codePoint) {
        if ('\u007F' < codePoint) {
            return false;
        }
        return true;
    }

    public static boolean isASCII(String str) {
        if (str == null || str.isEmpty()) {
            return true;
        }
        int len = str.length();
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (!isASCII(ch)) {
                return false;
            }
        }
        return true;
    }

    public static String toString(StringBuilder sb, boolean trim) {
        if (sb == null) {
            return null;
        }

        if (!trim) {
            return sb.toString();
        }

        int len = sb.length();
        if (len == 0) {
            return "";
        }

        int sIdx;
        for (sIdx = 0; sIdx < len; sIdx++) {
            char ch = sb.charAt(sIdx);
            if (!Character.isWhitespace(ch)) {
                break;
            }
        }

        if (sIdx == len) {
            return "";
        }

        int eIdx;
        for (eIdx = len - 1; 0 <= eIdx; eIdx--) {
            char ch = sb.charAt(eIdx);
            if (!Character.isWhitespace(ch)) {
                break;
            }
        }

        return sb.substring(sIdx, eIdx + 1);
    }
}

Related

  1. toCamelCase(String str)
  2. toCamelCase(String str)
  3. toCamelCase(String str)
  4. toCamelCase(String str)
  5. toCamelCase(String str)
  6. toCamelCase(String string)
  7. toCamelCase(String string)
  8. toCamelCase(String string)
  9. toCamelCase(String string)