un-Capitalize the first letter - Java java.lang

Java examples for java.lang:String Capitalize

Description

un-Capitalize the first letter

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String string = "java2s.com";
        System.out.println(unCapitalsFirst(string));
    }/*from   w w  w  .  j  a v a  2 s  .  c om*/

    private static final String TOLOWER = "lower";
    private static final String TOUPPER = "upper";

    /**
     * un-Capitalize the first letter
     * @param string the string to be un-capitalized  first letter
     * @return
     */
    public static String unCapitalsFirst(final String string) {
        return capitalsFirstOrUn(TOLOWER, string);
    }

    /**
     * Capitalize or un-Capitalize the first letter
     *
     * @param type "lower" --> Capptalize; "upper" --> un-Capitalize
     * @param string the string to be capitalized or un-capitalized  first letter
     * @return
     */
    private static String capitalsFirstOrUn(final String type,
            final String string) {
        if (string == null) {
            return null;
        }

        if (string.length() == 0) {
            return "";
        }

        StringBuffer sb = new StringBuffer();
        if (TOLOWER.equals(type)) {
            sb.append(string.substring(0, 1).toLowerCase());
        } else if (TOUPPER.equals(type)) {
            sb.append(string.substring(0, 1).toUpperCase());
        }

        if (string.length() > 1) {
            sb.append(string.substring(1));
        }

        return sb.toString();
    }
}

Related Tutorials