Java String Capitalize capitalize(String s)

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

Description

Capitalizes the given string, making its first character upper case, and the rest of them lower case.

License

Open Source License

Parameter

Parameter Description
s the string to capitalize

Return

the capitalized string

Declaration

public static String capitalize(String s) 

Method Source Code

//package com.java2s;
/*//from   w  ww. ja v  a 2s. c  o m
 * This file is part of muCommander, http://www.mucommander.com
 * Copyright (C) 2002-2010 Maxence Bernard
 *
 * muCommander is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * muCommander is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static final String EMPTY = "";

    /**
     * Capitalizes the given string, making its first character upper case, and the rest of them lower case.
     * This method reeturns an empty string if <code>null</code> or an empty string is passed.
     *
     * @param s the string to capitalize
     * @return the capitalized string
     */
    public static String capitalize(String s) {
        if (isNullOrEmpty(s))
            return EMPTY;

        StringBuilder out = new StringBuilder(s.length());
        out.append(Character.toUpperCase(s.charAt(0)));
        if (s.length() > 1)
            out.append(s.substring(1).toLowerCase());
        return out.toString();
    }

    /**
     * Returns true if the given string is null or empty (i.e. it's length is 0)
     *
     * @param string - the given String to check
     * @return true if the given string is null or empty, false otherwise
     */
    public static boolean isNullOrEmpty(String string) {
        return string == null || string.isEmpty();
    }
}

Related

  1. capitalize(String s)
  2. capitalize(String s)
  3. capitalize(String s)
  4. capitalize(String s)
  5. capitalize(String s)
  6. capitalize(String s)
  7. capitalize(String s)
  8. capitalize(String s)
  9. capitalize(String s)