Java String Capitalize First capitalizeFirstLetter(final String aString)

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

Description

Capitalize the first letter of every word in a string

License

Open Source License

Parameter

Parameter Description
aString the string to convert to Title case

Return

a new string with the first letter of every word capitalised

Declaration

public static String capitalizeFirstLetter(final String aString) 

Method Source Code

//package com.java2s;
/*//  w w  w  .  j  a v a2 s. c o m
 * Copyright 2002 (C) Bryan McRoberts <merton_monk@yahoo.com>
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *
 */

public class Main {
    /**
     * Capitalize the first letter of every word in a string
     * 
     * @param aString
     *            the string to convert to Title case
     * @return a new string with the first letter of every word capitalised
     */
    public static String capitalizeFirstLetter(final String aString) {
        boolean toUpper = true;
        final char[] a = aString.toLowerCase().toCharArray();

        for (int i = 0; i < a.length; ++i) {
            if (Character.isWhitespace(a[i])) {
                toUpper = true;
            } else {
                if (toUpper && Character.isLowerCase(a[i])) {
                    a[i] = Character.toUpperCase(a[i]);
                }

                toUpper = false;
            }
        }

        return new String(a);
    }
}

Related

  1. capitalizeFirstCharacters(String whole)
  2. capitalizeFirstCharacterString(String item)
  3. capitalizeFirstCharOfString(final String input)
  4. capitalizeFirstLeter(String sentence)
  5. capitalizeFirstLetter(final CharSequence chars)
  6. capitalizeFirstLetter(final String s)
  7. capitalizeFirstLetter(final String variableName)
  8. capitalizeFirstLetter(String data)
  9. capitalizeFirstLetter(String data)