to Hungarian Case - Java java.lang

Java examples for java.lang:String Case

Description

to Hungarian Case

Demo Code

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

public class Main {
    public static void main(String[] argv) {
        String str = "AbcDef";
        System.out.println(toHungarianCase(str));
    }//from w  w w  . java 2  s. co m

    public static String toHungarianCase(String str) {
        StringBuffer buffer = new StringBuffer(str);
        if (buffer.length() > 0) {
            Pattern p = Pattern.compile("[A-Z]");
            Matcher m = p.matcher(buffer.toString());
            while (m.find()) {
                String temp = m.group(); 
                int index = buffer.indexOf(temp); 
                buffer.replace(index, index + temp.length(),
                        (index > 0 ? "_" : "") + temp.toLowerCase());
            }
        }
        return buffer.toString();
    }
}

Related Tutorials