Java String Capitalize capitalise(String string)

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

Description

capitalise

License

Open Source License

Declaration

public static String capitalise(String string) 

Method Source Code

//package com.java2s;
/*/* w w w .j a  v a2  s .co m*/
 * This file is part of Commodus.
 *
 * Commodus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Commodus 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Commodus.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String capitalise(String string) {
        return capitalise(string, true);
    }

    /**
     * Capitalizes the first letter of a String
     *
     * @param string         the String to be capitalized
     * @param forceLowerCase whether to force lower case on other parts of the String
     * @return capitalized String
     */
    public static String capitalise(String string, boolean forceLowerCase) {
        String[] parts = string.split("\\s+");
        for (int i = 0; i < parts.length; i++) {
            if (parts[i].length() > 0) {
                parts[i] = parts[i].substring(0, 1).toUpperCase()
                        + (forceLowerCase ? parts[i].substring(1).toLowerCase() : parts[i].substring(1));
            }
        }
        return combineArray(0, " ", parts);
    }

    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(String separator, String... stringArray) {
        return combineArray(0, separator, stringArray);
    }

    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param startIndex  index to begin the separation at, inclusive
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(int startIndex, String separator, String... stringArray) {
        return combineArray(startIndex, stringArray.length, separator, stringArray);
    }

    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param startIndex  index to begin the separation at, inclusive
     * @param endIndex    index to end the separation at, exclusive
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(int startIndex, int endIndex, String separator, String... stringArray) {
        if (stringArray == null || startIndex >= endIndex) {
            return "";
        } else {
            StringBuilder builder = new StringBuilder();
            for (int i = startIndex; i < endIndex; i++) {
                builder.append(stringArray[i]);
                builder.append(separator);
            }
            builder.delete(builder.length() - separator.length(), builder.length());
            return builder.toString();
        }
    }
}

Related

  1. capitalise(String str)
  2. capitalise(String str)
  3. capitalise(String str)
  4. capitalise(String string)
  5. capitalise(String string)
  6. capitalise(String toCapitalise)
  7. capitaliseAndCheckBases(byte[] bases, boolean strict)
  8. capitalised(String name)
  9. capitaliseName(String name)