-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Expand file tree
/
Copy pathTitleCase.java
More file actions
54 lines (49 loc) · 1.65 KB
/
Copy pathTitleCase.java
File metadata and controls
54 lines (49 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.thealgorithms.strings;
/**
* Title Case converts a string so that the first letter of each word
* is capitalized and the rest are lowercase.
* Example: "the quick brown fox" -> "The Quick Brown Fox"
*
* @see <a href="https://en.wikipedia.org/wiki/Title_case">
* Wikipedia: Title Case</a>
*/
public final class TitleCase {
private TitleCase() {
// Utility class
}
/**
* Converts a string to title case.
*
* @param input The string to convert
* @return The title-cased string, or empty string if input is null/empty.
* If input contains only whitespace, it is returned as is.
*/
public static String toTitleCase(final String input) {
if (input == null || input.isEmpty()) {
return "";
}
StringBuilder result = new StringBuilder(input.length());
boolean capitalizeNext = true;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isWhitespace(c)) {
capitalizeNext = true;
result.append(c);
continue;
}
if (capitalizeNext) {
if (Character.isLetter(c)) {
result.append(Character.toUpperCase(c));
capitalizeNext = false;
} else {
// Keep capitalizeNext=true so the first *letter* after
// punctuation/digits is capitalized.
result.append(c);
}
} else {
result.append(Character.toLowerCase(c));
}
}
return result.toString();
}
}