Java String Capitalize capitalize(String string)

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

Description

Capitalizes the first letter of a String.

License

Open Source License

Parameter

Parameter Description
string the string to capitalize

Return

the capitalized string

Declaration

public static String capitalize(String string) 

Method Source Code

//package com.java2s;
/* Copyright 2010 - 2014 by Brian Uri!
       //from w  w w. j  a va  2  s .co  m
   This file is part of DDMSence.
       
   This library is free software; you can redistribute it and/or modify
   it under the terms of version 3.0 of the GNU Lesser General Public 
   License as published by the Free Software Foundation.
       
   This library 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 DDMSence. If not, see <http://www.gnu.org/licenses/>.
    
   You can contact the author at ddmsence@urizone.net. The DDMSence
   home page is located at http://ddmsence.urizone.net/
 */

public class Main {
    /**
     * Capitalizes the first letter of a String. Silently does nothing if the string is null, empty, or not a letter.
     * 
     * @param string the string to capitalize
     * @return the capitalized string
     */
    public static String capitalize(String string) {
        if (isEmpty(string))
            return (string);
        if (string.length() == 1)
            return (string.toUpperCase());
        return (string.substring(0, 1).toUpperCase() + string.substring(1, string.length()));
    }

    /**
     * Checks if a String value is empty. An empty string is defined as one that is null, contains only whitespace, or
     * has length 0.
     * 
     * @param value the value to check.
     * @return a boolean, true if the value is null or zero-length, false otherwise
     */
    public static boolean isEmpty(String value) {
        return (value == null || value.trim().length() == 0);
    }
}

Related

  1. capitalize(String str)
  2. capitalize(String str, char... delimiters)
  3. capitalize(String str, char[] wordDelimiters)
  4. capitalize(String str, String delim)
  5. capitalize(String str, String delimiter)
  6. capitalize(String string)
  7. capitalize(String string)
  8. capitalize(String string)
  9. capitalize(String string)