to Snake Case - Java java.lang

Java examples for java.lang:String Case

Description

to Snake Case

Demo Code


//package com.java2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static String toSnakeCase(String text) {
        String snake = text;//from w  w  w  .j  a  va2 s  .co m
        Pattern p = Pattern.compile("([A-Z])");
        for (;;) {
            Matcher m = p.matcher(snake);
            if (!m.find())
                break;
            snake = m.replaceFirst("_" + m.group(1).toLowerCase());
        }
        return snake.replaceFirst("^_", "");
    }
}

Related Tutorials