Here you can find the source of capitalize(String string)
Parameter | Description |
---|---|
string | The input string. |
public static String capitalize(String string)
//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(); } }