Java - Write code to Return the given string with the first letter converted to lower case.

Requirements

Write code to Return the given string with the first letter converted to lower case.

Demo

/*
 * Copyright (c) 2001-2002, Marco Hunsicker. All rights reserved.
 *
 * This software is distributable under the BSD license. See the terms of the
 * BSD license in the documentation provided with this software.
 *//*  www .  j  av a  2  s  .  c o m*/
 
//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "This is book2s.com";
        System.out.println(lowercaseFirst(str));
    }

    /**
     * Returns the given string with the first letter converted to lower case.
     *
     * @param str string to transform.
     *
     * @return the transformed string. If the string already begins with a lower case
     *         letter or is the empty string, the original string is returned.
     */
    public static String lowercaseFirst(String str) {
        if (str.length() == 0) {
            return str;
        }

        if (isUppercase(str.charAt(0))) {
            char[] letters = str.toCharArray();
            letters[0] = (char) (letters[0] + 32);

            return new String(letters);
        }

        return str;
    }

    /**
     * Returns <code>true</code> if the given letter is uppercase.
     *
     * @param letter letter to check for capitalization.
     *
     * @return <code>true</code> if the given letter is uppercase.
     */
    public static boolean isUppercase(char letter) {
        if ((letter < 'A') || (letter > 'Z')) {
            return false;
        }

        return true;
    }
}

Related Exercise