Java String Capitalize capitalize(String string)

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

Description

Capitalizes the string correctly.

License

Open Source License

Parameter

Parameter Description
string The input string.

Return

The string with correct capitalization.

Declaration

public static String capitalize(String string) 

Method Source Code

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

public class Main {
    /**/*from www  . ja v  a 2 s  .c o m*/
     * Capitalizes the string correctly.
     *
     * @param string The input string.
     * @return The string with correct capitalization.
     */
    public static String capitalize(String string) {
        boolean capitalize = true;
        StringBuilder builder = new StringBuilder(string);
        int length = string.length();

        for (int index = 0; index < length; index++) {
            char character = builder.charAt(index);

            if (character == '.' || character == '!' || character == '?') {
                capitalize = true;
            } else if (capitalize && !Character.isWhitespace(character)) {
                builder.setCharAt(index, Character.toUpperCase(character));
                capitalize = false;
            } else {
                builder.setCharAt(index, Character.toLowerCase(character));
            }
        }

        return builder.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)