Java - Write code to decapitalize a string


Write code to decapitalize a string

Abc should become abc.


Use Character.isUpperCase to check if the first letter is upper case.

Use Character.toLowerCase to convert the upper case to lower case.


//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "";
    }//from   w w w  . j a  v  a  2 s. com

    public static String decapitalize(String str) {
        if (isEmpty(str))
            return str;

        char cs[] = str.toCharArray();
        if (cs.length >= 2 && Character.isUpperCase(cs[0])
                && Character.isUpperCase(cs[1]))
            return str;

        cs[0] = Character.toLowerCase(cs[0]);
        return new String(cs);

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;