Java String Title Case toTitleCase2(String string, String separators)

Here you can find the source of toTitleCase2(String string, String separators)

Description

Convert a strong into an alternating form of each new word starting with a capital letter.

License

Open Source License

Parameter

Parameter Description
string a parameter
separators a parameter

Return

new string

Declaration

public static String toTitleCase2(String string, String separators) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  w  w  w. ja va2  s  . c o  m
     * Convert a strong into an alternating form of each new word starting with a capital letter.
     *
     * @param string
     * @param separators
     * @return new string
     */
    public static String toTitleCase2(String string, String separators) {
        char[] seps = separators.toCharArray();
        char[] tempChar = string.toLowerCase().toCharArray();
        boolean firstLetter = false;
        boolean hitSep = false;
        int i, j;

        // The first character is always upper cased
        tempChar[0] = Character.toUpperCase(tempChar[0]);

        // Loop through the rest of them
        for (i = 1; i < string.length(); i++) {
            if (firstLetter) {
                tempChar[i] = Character.toUpperCase(tempChar[i]);
                firstLetter = false;
            } else {
                // Check if we hit a separator
                for (j = 0; j < seps.length; j++) {
                    if (tempChar[i] == seps[j]) {
                        hitSep = true;
                        break;
                    }
                }
                if (hitSep) {
                    tempChar[i] = ' ';
                    firstLetter = true;
                    hitSep = false;
                }
            }

        }
        return new String(tempChar);
    }
}

Related

  1. toTitleCase(String str)
  2. toTitleCase(String string)
  3. toTitleCase(String string)
  4. toTitleCase(String text)
  5. toTitleCase(String title)
  6. toTitleCaseIdentifier(String formStr)