Here you can find the source of format(String name, char separator)
protected static String format(String name, char separator)
//package com.java2s; /**/*from w w w . j a v a2 s. co m*/ * Copyright (c) 2010 Kenn Hussey and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * Kenn Hussey - Initial API and implementation */ import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main { protected static String format(String name, char separator) { StringBuffer result = new StringBuffer(); for (Iterator<String> i = parseName(name, '_').iterator(); i.hasNext();) { String component = i.next(); result.append(component); if (i.hasNext() && component.length() > 1) { result.append(separator); } } return result.toString(); } protected static List<String> parseName(String sourceName, char sourceSeparator) { List<String> result = new ArrayList<String>(); StringBuffer currentWord = new StringBuffer(); int length = sourceName.length(); boolean lastIsLower = false; for (int index = 0; index < length; index++) { char curChar = sourceName.charAt(index); if (Character.isUpperCase(curChar) || (!lastIsLower && Character.isDigit(curChar)) || curChar == sourceSeparator) { if (lastIsLower || curChar == sourceSeparator) { result.add(currentWord.toString()); currentWord = new StringBuffer(); } lastIsLower = false; } else { if (!lastIsLower) { int currentWordLength = currentWord.length(); if (currentWordLength > 1) { char lastChar = currentWord.charAt(--currentWordLength); currentWord.setLength(currentWordLength); result.add(currentWord.toString()); currentWord = new StringBuffer(); currentWord.append(lastChar); } } lastIsLower = true; } if (curChar != sourceSeparator) { currentWord.append(curChar); } } result.add(currentWord.toString()); return result; } }