Java String Capitalize Word capitalizeFirstLetter(final String word)

Here you can find the source of capitalizeFirstLetter(final String word)

Description

Capitalizes the first letter of the passed in string.

License

Apache License

Parameter

Parameter Description
word a parameter

Declaration

public static String capitalizeFirstLetter(final String word) 

Method Source Code

//package com.java2s;
/*/*  w w  w.j  av  a  2 s.  c om*/
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Capitalizes the first letter of the passed in string. If the passed word
     * is an empty word or contains only whitespace characters, then this passed
     * word is returned. If the first letter is already capitalized returns the
     * passed word. Otherwise capitalizes the first letter of the this word.
     * 
     * @param word
     * @return
     */
    public static String capitalizeFirstLetter(final String word) {
        if (isEmpty(word))
            return word;
        if (Character.isUpperCase(word.charAt(0)))
            return word;
        if (word.length() == 1)
            return word.toUpperCase();
        return Character.toUpperCase(word.charAt(0)) + word.substring(1);
    }

    public static boolean isEmpty(final String string) {
        if (string == null || string.length() == 0) {
            return true;
        }

        for (int i = 0; i < string.length(); i++) {
            if (!Character.isWhitespace(string.charAt(i))) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. capitalizedWord(String s)
  2. capitalizedWords(String s)
  3. CapitalizeEachWord(String Str)
  4. capitalizeEachWord(String string)
  5. capitalizeFirstCharWords(String sentence)
  6. capitalizeFirstLetter(String word)
  7. capitalizeFirstLetterEachWord(String sentence)
  8. capitalizeFirstLetterWord(String word)
  9. capitalizeFirsts(String word)