Java String Capitalize All capitalizeAll(final String value)

Here you can find the source of capitalizeAll(final String value)

Description

Transforms all the first letters of words in UpperCase (Capitalized)

License

Open Source License

Parameter

Parameter Description
value - value to capitalized

Return

the value capitalized

Declaration

public static String capitalizeAll(final String value) 

Method Source Code

//package com.java2s;
/*/*w  w w  .  j  av  a  2 s.  c o  m*/
 *     @(#)StringUtils.java   1.4 10/11/22
 * 
 *   Copyright (c) 2010 Felipe Priuli
 *
 *   This file is part of OpenSutils-Br4J.
 *
 *   OpenSutils-Br4J 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, any later version.
 *
 *   OpenSutils-Br4J 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    /**
     * Transforms all the first letters of words in UpperCase (Capitalized)
     * @param value - value to capitalized
     * @return the value capitalized
     */
    public static String capitalizeAll(final String value) {
        if (value == null)
            return null;
        if (value.trim().isEmpty())
            return value;

        StringBuilder sb = new StringBuilder(value.length());

        char[] arr = value.toCharArray();
        boolean flagUpper = false;
        for (int i = 0; i < arr.length; i++) {
            char c = arr[i];
            switch (c) {
            case 13:
            case 32:
            case 10:
                flagUpper = true;
                sb.append(c);
                break;
            default:
                if (flagUpper) {
                    sb.append(Character.toUpperCase(c));
                    flagUpper = false;
                } else
                    sb.append(c);
                break;
            }
        }
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        return sb.toString();
    }
}

Related

  1. capitaliseAll(String str)
  2. capitaliseAllWords(String str)
  3. capitaliseAllWords(String str)
  4. capitaliseAllWords(String str)
  5. capitalizeAll(String string)
  6. capitalizeAll(String text)
  7. capitalizeAllFirstChar(String text)
  8. capitalizeAllWords(String text)