Camel Case Generator in Java

How to Generate Camel Case in Java

In this example, We will show you simple java program about, How to generate camel case in Java. The program has been tested and output shared in the same post.

Example Program

package com.dineshkrish;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class Generator {

	public static enum Token {

		// Enum Constant
		SPACE(" "), COMMA(","), DOT("\\."), DASH("-"), UNDERSCORE("_");

		private String token;

		Token(String token) {

			this.token = token;
		}

		public String getToken() {

			return token;
		}
	}

	public static String camelCase(String str, Token token) {

		String result = "";

		if (str != null && !str.isEmpty()) {

			String[] words = str.split(token.getToken());

			for (int i = 0; i < words.length; i++) {

				if(i == 0) {
					result += words[i].toLowerCase(); // converting first word to lower case
				} else {
					result += words[i].substring(0, 1).toUpperCase() + words[i].substring(1); // converting remaining words are capitalize
				}
			}
		}

		return result;
	}

	public static void main(String[] args) {

		System.out.println(camelCase("Get customer address", Token.SPACE));
		System.out.println(camelCase("Get-Product-Information", Token.DASH));
		System.out.println(camelCase("set_Customer_Name", Token.UNDERSCORE));
		System.out.println(camelCase("is.Real.Customer", Token.DOT));

	}

}

Output

getCustomerAddress
getProductInformation
setCustomerName
isRealCustomer
Hello everyone, I am a Founder of dineshkrish.com.