Java String Snake Case toSnakeCase(String camelCase)

Here you can find the source of toSnakeCase(String camelCase)

Description

to Snake Case

License

Open Source License

Declaration

public static String toSnakeCase(String camelCase) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final String SNAKE_CASE_SPLIT_REGEX = "(?<!^)(?<![A-Z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])";

    public static String toSnakeCase(String camelCase) {
        if (camelCase == null) {
            throw new NullPointerException();
        }//from   w w  w .j ava  2  s  . c  o m

        String[] parts = camelCase.split(SNAKE_CASE_SPLIT_REGEX);

        StringBuilder builder = new StringBuilder(camelCase.length()
                + (parts.length - 1));

        for (int i = 0; i < parts.length - 1; i++) {
            builder.append(parts[i].toLowerCase()).append('_');
        }
        builder.append(parts[parts.length - 1].toLowerCase());

        return builder.toString();
    }
}

Related

  1. toSnakeCase(String s)
  2. toSnakeCase(String src)
  3. toSnakeCase(String str)
  4. toSnakeCase(String str)