Convert String to Upper Snake Case - Java java.lang

Java examples for java.lang:String Case

Description

Convert String to Upper Snake Case

Demo Code


//package com.java2s;

public class Main {


    public static String toUpperSnakeCase(String variableName) {
        StringBuilder builder = new StringBuilder(); // TODO: max num snake_case chars?
        char[] nameChars = variableName.toCharArray();
        for (int i = 0; i < nameChars.length; i++) {
            char ch = nameChars[i];
            if (i != 0 && Character.isUpperCase(ch)) {
                builder.append('_').append(ch);
            } else {
                builder.append(Character.toUpperCase(ch));
            }//from  w  w  w .  j  a va2 s . c o m
        }
        return builder.toString();
    }
}

Related Tutorials