Uncapitalizes a String changing the first letter to title case as per Character#toLowerCase(char) . - Java java.lang

Java examples for java.lang:String Capitalize

Introduction

The following code shows how to Uncapitalizes a String changing the first letter to title case as per Character#toLowerCase(char) . .

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        System.out.println(uncapitalize(str));
    }/*from  w  w  w  . j  ava  2s  . c o m*/

    /**
     * <p>
     * Uncapitalizes a String changing the first letter to title case as per
     * {@link Character#toLowerCase(char)}. No other letters are changed.
     * </p>
     * 
     * <pre>
     * StringUtils.uncapitalize(null)  = null
     * StringUtils.uncapitalize("")    = ""
     * StringUtils.uncapitalize("Cat") = "cat"
     * StringUtils.uncapitalize("CAT") = "cAT"
     * </pre>
     * 
     * @param str
     *            the String to uncapitalize, may be null
     * @return the uncapitalized String, <code>null</code> if null String input
     * @see #capitalize(String)
     * @since 2.0
     */
    public static String uncapitalize(String str) {
        if (str == null) {
            return null;
        }
        StringBuilder result = new StringBuilder(str);

        if (result.length() > 0) {
            result.setCharAt(0, Character.toLowerCase(result.charAt(0)));
        }

        return result.toString();
    }
}

Related Tutorials