Java String Capitalize capitalize(String string)

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

Description

Converts the string to capitalized.

License

Open Source License

Parameter

Parameter Description
string the string to capitalize.

Return

the capitalized string.

Declaration

public static String capitalize(String string) 

Method Source Code

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

public class Main {
    /**//from ww  w  .  j  a v a  2  s  .c om
     * Converts the string to capitalized. Strings are of the format: THIS_IS_A_STRING, and the result of capitalization
     * would be ThisIsAString.
     * 
     * @param string the string to capitalize.
     * @return the capitalized string.
     * @since 2.0
     */
    public static String capitalize(String string) {
        StringBuffer result = new StringBuffer(toCamelCase(string));
        result.replace(0, 1, "" + Character.toUpperCase(result.charAt(0))); //$NON-NLS-1$
        return result.toString();
    }

    /**
     * Converts the string to camelcase. Strings are of the format: THIS_IS_A_STRING, and the result of camel casing
     * would be thisIsAString.
     * 
     * @param string the string to be camelcased.
     * @return the camel cased string.
     * @since 2.0
     */
    public static String toCamelCase(String string) {
        StringBuffer result = new StringBuffer(string);
        while (result.indexOf("_") != -1) { //$NON-NLS-1$
            int indexOf = result.indexOf("_"); //$NON-NLS-1$
            result.replace(indexOf, indexOf + 2, "" + Character.toUpperCase(result.charAt(indexOf + 1))); //$NON-NLS-1$
        }
        return result.toString();
    }
}

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)
  7. capitalize(String string)
  8. capitalize(String string)
  9. capitalize(String string)