In Java, Character.toTitleCase(ch) returns the character's titlecase form: the shape it should have as the first letter of a capitalised word. For ordinary letters that is just the uppercase letter, so toTitleCase('a') is 'A'.
The difference shows up for a handful of digraph characters. The lowercase digraph U+01C6 has the uppercase form U+01C4 (both parts capital) and the titlecase form U+01C5 (only the first part capital). If Unicode lists no titlecase mapping, the uppercase mapping is used.
A static method of java.lang.Character (Java 1.0+): call it on the class, as in Character.toTitleCase(...); you do not need a Character object. No import is needed, because java.lang is always available. The int codePoint overload was added in Java 5.
Unicode version.
The answer comes from the Unicode Character Database built into your JDK, and each Java release follows a specific Unicode version (Java 8, for example, uses Unicode 6.2). The characters used on this page have had the same properties for many Unicode versions, so the examples print the same result on any current JDK.
Syntax
Same shape as the other case conversions: char in, char out, or int in, int out.
Java
char a = Character.toTitleCase('a'); // 'A', same as toUpperCase
char b = Character.toTitleCase('\u01C6'); // '\u01C5', capital D + small z with caron
char c = Character.toUpperCase('\u01C6'); // '\u01C4', both parts capital
char d = Character.toTitleCase('9'); // '9', no case
Call
What it does
Character.toTitleCase(word.charAt(0))
Capitalise the first character of a word.
Character.toTitleCase(cp)
The code point version.
Character.isTitleCase(Character.toTitleCase(c))
True only when a special titlecase letter exists.
Overloads: char toTitleCase(char ch) and int toTitleCase(int codePoint). There is no String.toTitleCase() in the JDK; you combine this method with toLowerCase yourself.
Library declarations
Java
// java.lang.Character
public static char toTitleCase(char ch)
public static int toTitleCase(int codePoint) // since Java 5
Parameters
Parameter
Description
ch (char)
The character to convert.
codePoint (int)
The code point to convert.
Return Value
Return Value
Description
char / int
The titlecase form; the uppercase form when no titlecase mapping exists; the argument itself when it has no case.
Titlecase the first letter of each word and lowercase the rest.
Java
public class CharacterToTitleCasePhrase {
public static void main(String[] args) {
String phrase = "hELLO wORLD";
StringBuilder out = new StringBuilder();
boolean first = true;
for (char c : phrase.toCharArray()) {
if (c == ' ') {
out.append(c);
first = true;
} else {
out.append(first ? Character.toTitleCase(c) : Character.toLowerCase(c));
first = false;
}
}
System.out.println(out);
}
}
Output
Hello World
How It Works
Two conversions per word. The first letter goes through toTitleCase; every later letter goes through toLowerCase.
Spaces reset the state. A space is copied and sets first back to true, so w becomes W.
Same as uppercase here. For h and w, titlecase and uppercase are identical; the method only matters for digraphs, but it is the semantically right call for "first letter of a word".
Example 2 — Every Form of the Digraph Maps to U+01C5
Apply toTitleCase and toUpperCase to the uppercase, titlecase, and lowercase forms of one digraph.
Java
public class CharacterToTitleCaseDigraph {
public static void main(String[] args) {
char[] forms = {'\u01C4', '\u01C5', '\u01C6'};
for (char c : forms) {
System.out.println((int) c + " title=" + (int) Character.toTitleCase(c)
+ " upper=" + (int) Character.toUpperCase(c));
}
}
}
Supplementary letters work. DESERET SMALL LETTER LONG I (0x10428) has no separate titlecase mapping, so its uppercase U+10400 is returned.
Hex output.Integer.toHexString prints 'z' as 7a and its titlecase 'Z' as 5a.
No single-character answer. The digit has no case, and sharp s has no one-character titlecase form, so both come back unchanged.
Frequently Asked Questions
For almost every character they return the same result. They differ for digraphs such as U+01C6, where titlecase capitalises only the first part.
No. Loop over the words yourself: apply Character.toTitleCase to the first character and Character.toLowerCase to the rest.
toTitleCase('a') returns 'A', which belongs to the UPPERCASE_LETTER category. Only digraph-style characters are TITLECASE_LETTER.
No. It uses Unicode data only and ignores the default locale.
It is the most correct single-character choice for the first letter. For names you may also need locale rules and exceptions such as McDonald, which no single method handles.