Java - Write code to decamelize a string

Requirements

Write code to decamelize a string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "book2s.com AbcDef";
        System.out.println(decamelize(s));
    }//from  w w w. j a v a  2 s .  c o m

    public static String decamelize(final String s) {
        if (s == null) {
            return null;
        }
        if (s.length() == 1) {
            return s.toUpperCase();
        }
        StringBuffer buf = new StringBuffer(40);
        int pos = 0;
        for (int i = 1; i < s.length(); ++i) {
            if (Character.isUpperCase(s.charAt(i))) {
                if (buf.length() != 0) {
                    buf.append('_');
                }
                buf.append(s.substring(pos, i).toUpperCase());
                pos = i;
            }
        }
        if (buf.length() != 0) {
            buf.append('_');
        }
        buf.append(s.substring(pos, s.length()).toUpperCase());
        return buf.toString();
    }
}

Related Exercise