Here you can find the source of capitalize(final String s)
Parameter | Description |
---|---|
s | The string to be capitalized. |
public static String capitalize(final String s)
//package com.java2s; /*// w w w .j a v a 2 s .c o m * Copyright (C) 2008-2018 Ping Identity Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License (GPLv2 only) * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see <http://www.gnu.org/licenses>. */ public class Main { /** * Capitalizes the provided string. The first character will be converted to * uppercase, and the rest of the string will be left unaltered. * * @param s The string to be capitalized. * * @return A capitalized version of the provided string. */ public static String capitalize(final String s) { return capitalize(s, false); } /** * Capitalizes the provided string. The first character of the string (or * optionally the first character of each word in the string) * * @param s The string to be capitalized. * @param allWords Indicates whether to capitalize all words in the string, * or only the first word. * * @return A capitalized version of the provided string. */ public static String capitalize(final String s, final boolean allWords) { if (s == null) { return null; } switch (s.length()) { case 0: return s; case 1: return s.toUpperCase(); default: boolean capitalize = true; final char[] chars = s.toCharArray(); final StringBuilder buffer = new StringBuilder(chars.length); for (final char c : chars) { // Whitespace and punctuation will be considered word breaks. if (Character.isWhitespace(c) || (((c >= '!') && (c <= '.')) || ((c >= ':') && (c <= '@')) || ((c >= '[') && (c <= '`')) || ((c >= '{') && (c <= '~')))) { buffer.append(c); capitalize |= allWords; } else if (capitalize) { buffer.append(Character.toUpperCase(c)); capitalize = false; } else { buffer.append(c); } } return buffer.toString(); } } /** * Retrieves an all-uppercase version of the provided string. * * @param s The string for which to retrieve the uppercase version. * * @return An all-uppercase version of the provided string. */ public static String toUpperCase(final String s) { if (s == null) { return null; } final int length = s.length(); final char[] charArray = s.toCharArray(); for (int i = 0; i < length; i++) { switch (charArray[i]) { case 'a': charArray[i] = 'A'; break; case 'b': charArray[i] = 'B'; break; case 'c': charArray[i] = 'C'; break; case 'd': charArray[i] = 'D'; break; case 'e': charArray[i] = 'E'; break; case 'f': charArray[i] = 'F'; break; case 'g': charArray[i] = 'G'; break; case 'h': charArray[i] = 'H'; break; case 'i': charArray[i] = 'I'; break; case 'j': charArray[i] = 'J'; break; case 'k': charArray[i] = 'K'; break; case 'l': charArray[i] = 'L'; break; case 'm': charArray[i] = 'M'; break; case 'n': charArray[i] = 'N'; break; case 'o': charArray[i] = 'O'; break; case 'p': charArray[i] = 'P'; break; case 'q': charArray[i] = 'Q'; break; case 'r': charArray[i] = 'R'; break; case 's': charArray[i] = 'S'; break; case 't': charArray[i] = 'T'; break; case 'u': charArray[i] = 'U'; break; case 'v': charArray[i] = 'V'; break; case 'w': charArray[i] = 'W'; break; case 'x': charArray[i] = 'X'; break; case 'y': charArray[i] = 'Y'; break; case 'z': charArray[i] = 'Z'; break; default: if (charArray[i] > 0x7F) { return s.toUpperCase(); } break; } } return new String(charArray); } }