Java String Camel Case Format toCamelCase(String s)

Here you can find the source of toCamelCase(String s)

Description

to Camel Case

License

Open Source License

Declaration

public static String toCamelCase(String s) 

Method Source Code

//package com.java2s;
/*/*from  www.  j a v  a 2 s.c om*/
 * Copyright 2015, Yahoo Inc.
 * Copyrights licensed under the Apache 2.0 License.
 * See the accompanying LICENSE file for terms.
 */

public class Main {
    public static String toCamelCase(String s) {
        String[] parts = s.split("_");
        if (parts.length > 1) {
            return convertComponentsToCamelCaseName(parts);
        } else if (isAllUppercase(s)) {
            s = s.toLowerCase();
        }
        return s;
    }

    private static String convertComponentsToCamelCaseName(String[] parts) {
        StringBuilder result = new StringBuilder();
        for (String s : parts) {
            s = s.toLowerCase();
            if (s.length() != 0) {
                if (result.length() == 0) {
                    result.append(s);
                } else {
                    result.append(capitalize(s));
                }
            }
        }
        return result.toString();
    }

    private static boolean isAllUppercase(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (!Character.isUpperCase(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static String capitalize(String s) {
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }
}

Related

  1. toCamelCase(String name)
  2. toCamelCase(String original)
  3. toCamelCase(String originalName)
  4. toCamelCase(String origString)
  5. toCamelCase(String s)
  6. toCamelCase(String s)
  7. toCamelCase(String s)
  8. toCamelCase(String s)
  9. toCamelCase(String s)