Java String Snake Case toSnakeCase(String str, boolean capitalize, Boolean upper)

Here you can find the source of toSnakeCase(String str, boolean capitalize, Boolean upper)

Description

to Snake Case

License

MIT License

Declaration

public static String toSnakeCase(String str, boolean capitalize, Boolean upper) 

Method Source Code

//package com.java2s;
/*!// w  ww . ja v a  2 s.  c o m
 * 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 toSnakeCase(String str, boolean capitalize, Boolean upper) {
        return changeSeparator(str, '_', capitalize, null, upper);
    }

    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. toSnakeCase(String camelCase)
  2. toSnakeCase(String s)
  3. toSnakeCase(String src)
  4. toSnakeCase(String str)
  5. toSnakeCase(String str)