-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Expand file tree
/
Copy pathTitleCase.java
More file actions
43 lines (40 loc) · 1.3 KB
/
Copy pathTitleCase.java
File metadata and controls
43 lines (40 loc) · 1.3 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
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();
boolean capitalizeNext = true;
for (char c : input.toCharArray()) {
if (Character.isWhitespace(c)) {
capitalizeNext = true;
result.append(c);
} else if (capitalizeNext) {
result.append(Character.toUpperCase(c));
capitalizeNext = false;
} else {
result.append(Character.toLowerCase(c));
}
}
return result.toString();
}
}