Java String Capitalize capitalize(String string)

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

Description

Capitalize the first letter of a string.

License

Mozilla Public License

Parameter

Parameter Description
string the string

Return

p_string with the first letter capitalized

Declaration

public static String capitalize(String string) 

Method Source Code

//package com.java2s;
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

public class Main {
    /**//  ww  w .  j ava 2 s .  c o m
     * Capitalize the first letter of a string. If the string is empty, just return an empty string;
     * if it is null, return a null string. string.
     *
     * @param string the string
     * @return {@code p_string} with the first letter capitalized
     */
    public static String capitalize(String string) {
        if (string == null) {
            return null;
        } else if (string.length() == 0) {
            return string;
        } else {
            char[] chars = string.toCharArray();
            chars[0] = Character.toUpperCase(chars[0]);
            return new String(chars);
        }
    }
}

Related

  1. capitalize(String str, String delim)
  2. capitalize(String str, String delimiter)
  3. capitalize(String string)
  4. capitalize(String string)
  5. capitalize(String string)
  6. capitalize(String string)
  7. capitalize(String string)
  8. capitalize(String string)
  9. capitalize(String string)