Java String Capitalize capitalize(final String s)

Here you can find the source of capitalize(final String s)

Description

Capitalizes the first character of the specified string, and de-capitalize the rest of characters.

License

Open Source License

Parameter

Parameter Description
s the original word

Return

the capitalized word

Declaration

public static String capitalize(final String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 LegSem./* w w  w. j  a  v a  2 s. com*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * Capitalizes the first character of the specified string,
     * and de-capitalize the rest of characters.
     * 
     * @param s the original word
     * @return the capitalized word
     */
    public static String capitalize(final String s) {
        if (!isLower(s.charAt(0))) {
            return s;
        }
        StringBuilder sb = new StringBuilder(s.length());
        sb.append(Character.toUpperCase(s.charAt(0)));
        sb.append(s.substring(1).toLowerCase());
        return sb.toString();
    }

    /**
     * Determine if character is lowercase.
     * 
     * @param c the character to test
     * @return true if lower case
     */
    protected static boolean isLower(final char c) {
        return c >= 'a' && c <= 'z' || Character.isLowerCase(c);
    }
}

Related

  1. capitalize(CharSequence s)
  2. capitalize(CharSequence s)
  3. capitalize(final String docName)
  4. capitalize(final String input)
  5. capitalize(final String name)
  6. capitalize(final String s)
  7. capitalize(final String s)
  8. capitalize(final String s)
  9. capitalize(final String s)