Java String Capitalize capitalize(String str, char[] wordDelimiters)

Here you can find the source of capitalize(String str, char[] wordDelimiters)

Description

Capitalize first letter of each word.

License

Creative Commons License

Parameter

Parameter Description
str a parameter
wordDelimiters a parameter

Declaration

public static String capitalize(String str, char[] wordDelimiters) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

public class Main {
    /**//from   w w w.  j  ava2 s .  c  om
     * Capitalize first letter of each word.
     * 
     * @param str
     * @param wordDelimiters
     * @return
     */
    public static String capitalize(String str, char[] wordDelimiters) {
        int strLen = str.length();
        StringBuffer buffer = new StringBuffer(strLen);
        boolean capitalizeNext = true;
        for (int i = 0; i < strLen; i++) {
            char ch = str.charAt(i);

            boolean isDelimiter = false;
            for (int j = 0, isize = wordDelimiters.length; j < isize; j++) {
                if (ch == wordDelimiters[j]) {
                    isDelimiter = true;
                }
            }

            if (isDelimiter) {
                buffer.append(ch);
                capitalizeNext = true;
            } else if (capitalizeNext) {
                buffer.append(Character.toTitleCase(ch));
                capitalizeNext = false;
            } else {
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }
}

Related

  1. capitalize(String str)
  2. capitalize(String str)
  3. capitalize(String str)
  4. capitalize(String str)
  5. capitalize(String str, char... delimiters)
  6. capitalize(String str, String delim)
  7. capitalize(String str, String delimiter)
  8. capitalize(String string)
  9. capitalize(String string)