Java - Write code to camelize a string by separator

Requirements

Write code to camelize a string by separator

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "book2s.com";
        String separator = ".";
        System.out.println(camelize(s, separator));
    }//  ww  w.j av  a2s .c o m

    public static String camelize(String s, String separator) {
        String[] parts = s.split(separator);
        String camelCaseString = "";
        for (String part : parts) {
            camelCaseString = camelCaseString + capitalize(part, false);
        }
        return camelCaseString;
    }

    public static String capitalize(String s, boolean preserveCase) {
        if (s.length() == 0) {
            return s;
        }
        if (preserveCase) {
            return s.substring(0, 1).toUpperCase() + s.substring(1);
        } else {
            return s.substring(0, 1).toUpperCase()
                    + s.substring(1).toLowerCase();
        }

    }
}

Related Exercise