Java String Capitalize capitalize(String string, boolean firstWordOnly)

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

Description

Capitalizes the first character in either the first word or all words in the given string.

License

Open Source License

Parameter

Parameter Description
string The string to capitalize.
firstWordOnly Whether only the first word should be capitalized, or all words.

Return

The capitalized string.

Declaration

public static String capitalize(String string, boolean firstWordOnly) 

Method Source Code

//package com.java2s;
/*/*w w  w.  j  a v a 2  s  .c  o m*/
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

public class Main {
    /**
     * Capitalizes the first character in either the first word or all words in the given string.
     *
     * @param string        The string to capitalize.
     * @param firstWordOnly Whether only the first word should be capitalized, or all words.
     * @return              The capitalized string.
     */
    public static String capitalize(String string, boolean firstWordOnly) {
        if (string == null)
            return null;

        char[] characters = string.toCharArray();
        boolean capitalize = true;

        for (int i = 0; i < characters.length; i++) {
            char character = characters[i];
            if (Character.isWhitespace(character)) {
                capitalize = true;
            } else if (capitalize) {
                characters[i] = Character.toTitleCase(character);
                capitalize = false;
                if (firstWordOnly)
                    break;
            }
        }

        return new String(characters);
    }

    /**
     * Capitalizes the first character in either the first word or all words in each of the given
     * strings.
     *
     * @param input         The strings to capitalize.
     * @param firstWordOnly Whether only the first word should be capitalized, or all words.
     * @return              The capitalized strings.
     */
    public static String[] capitalize(String[] input, boolean firstWordOnly) {
        if (input == null)
            return null;

        String[] output = new String[input.length];

        for (int i = 0; i < input.length; i++) {
            output[i] = capitalize(input[i], firstWordOnly);
        }

        return output;
    }
}

Related

  1. capitalize(String string)
  2. capitalize(String string)
  3. capitalize(String string)
  4. capitalize(String string)
  5. capitalize(String string)
  6. capitalize(String string, int index)
  7. capitalize(String string0)
  8. capitalize(String text)
  9. capitalize(String text)