Here you can find the source of capitalize(String string)
Parameter | Description |
---|---|
string | string to capitalize |
public static String capitalize(String string)
//package com.java2s; //License from project: Open Source License public class Main { /**/* w ww . j a v a2 s .c om*/ * Capitalize string. * * @param string string to capitalize * @return capitalized string */ public static String capitalize(String string) { if (string == null || string.isEmpty()) { return string; } int index = 0, length = string.length(); while (index < length - 1) { if (Character.isAlphabetic(string.charAt(index))) { break; } ++index; } if (index == length) { return string; } else { StringBuilder builder = new StringBuilder(); builder.append(string.substring(0, index)); builder.append(Character.toUpperCase(string.charAt(index))); builder.append(string.substring(index + 1)); return builder.toString(); } } /** * Append string to line. * * @param line original line * @param append string to append * @return fixed string */ public static String append(String line, String append) { return prependAndAppend(line, "", append); } /** * Prepend string to beginning of line and append string to end of line. * * @param line original line * @param prepend string to prepend * @param append string to append * @return fixed line */ public static String prependAndAppend(String line, String prepend, String append) { StringBuilder builder = new StringBuilder(); builder.append(prepend); builder.append(line); builder.append(append); return builder.toString(); } }