Returns the given string, with its first letter changed to lower-case unless the string starts with more than one upper-case letter, in which case the string will be returned unaltered. - Java java.lang

Java examples for java.lang:String Case

Description

Returns the given string, with its first letter changed to lower-case unless the string starts with more than one upper-case letter, in which case the string will be returned unaltered.

Demo Code

/*//w  w w.j  av a  2 s  . c o m
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String string = "java2s.com";
        System.out.println(decapitalize(string));
    }

    /**
     * Returns the given string, with its first letter changed to lower-case unless the string starts with more than
     * one upper-case letter, in which case the string will be returned unaltered.
     * <p>
     * Provided to avoid a dependency on the {@link java.beans.Introspector} API which is not available on the Android
     * platform (HV-779).
     *
     * @param string the string to decapitalize
     *
     * @return the given string, decapitalized. {@code null} is returned if {@code null} is passed as input; An empty
     *         string is returned if an empty string is passed as input
     *
     * @see java.beans.Introspector#decapitalize(String)
     */
    public static String decapitalize(String string) {
        if (string == null || string.isEmpty()
                || startsWithSeveralUpperCaseLetters(string)) {
            return string;
        } else {
            return string.substring(0, 1).toLowerCase()
                    + string.substring(1);
        }
    }

    private static boolean startsWithSeveralUpperCaseLetters(String string) {
        return string.length() > 1
                && Character.isUpperCase(string.charAt(0))
                && Character.isUpperCase(string.charAt(1));
    }
}

Related Tutorials