Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (c) 2013, 2014 Denis Nikiforov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Denis Nikiforov - initial API and implementation
 */

public class Main {
    /**
     * Converts a string that contains upper-case letter and underscores (e.g.,
     * constant names) to a camel-case string. For example, MY_CONSTANT is converted
     * to myConstant.
     * 
     * @param text the string to convert
     * 
     * @return a camel-case version of text
     */
    public static String convertAllCapsToLowerCamelCase(String text) {
        String lowerCase = text.toLowerCase();
        while (true) {
            int i = lowerCase.indexOf('_');
            if (i < 0) {
                break;
            }
            String head = lowerCase.substring(0, i);
            if (i + 1 == lowerCase.length()) {
                lowerCase = head;
                break;
            } else {
                char charAfterUnderscore = lowerCase.charAt(i + 1);
                char upperCase = Character.toUpperCase(charAfterUnderscore);
                if (i + 2 < lowerCase.length()) {
                    String tail = lowerCase.substring(i + 2, lowerCase.length());
                    lowerCase = head + upperCase + tail;
                } else {
                    lowerCase = head + upperCase;
                    break;
                }
            }
        }
        return lowerCase;
    }
}