to Camel Case - Java java.lang

Java examples for java.lang:String Camel Case

Description

to Camel Case

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String s = "this is a test. Test";
        System.out.println(toCamelCase(s));
    }/*w  ww  .  j  a v a  2 s.  c om*/

    public static String toCamelCase(String s, boolean firstCharUppercase) {
        char[] cl = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        boolean upper = firstCharUppercase;
        for (char ch : cl) {
            switch (ch) {
            case '_':
            case '?':
            case ';': // add anything to escape
                upper = true;
                break;
            default:
                sb.append((char) (upper ? (ch >= 'a' && ch <= 'z' ? ch - 'a' + 'A'
                        : ch)
                        : ch));
                upper = false;
            }
        }
        return sb.toString();
    }

    public static String toCamelCase(String s) {
        if (s.toLowerCase().equals("id"))
            return "Id";
        return toCamelCase(s, true);
    }
}

Related Tutorials