Added capitalizeWords renderer

This commit is contained in:
Erik C. Thauvin 2024-11-15 11:47:40 -08:00
parent daadd0bc4a
commit 99f6afe4fb
Signed by: erik
GPG key ID: 776702A6A2DA330E
4 changed files with 103 additions and 9 deletions

View file

@ -124,6 +124,38 @@ public final class RenderUtils {
return String.format("@%03d", beats);
}
/**
* Returns a {@code String} with the first letter of each word capitalized.
*
* @param src the source {@code String}
* @return the capitalized {@code String}
*/
public static String capitalizeWords(String src) {
if (src == null || src.isBlank()) {
return src;
}
var result = new StringBuilder();
var capitalizeNext = true;
for (var i = 0; i < src.length(); i++) {
char c = src.charAt(i);
if (Character.isWhitespace(c)) {
capitalizeNext = true;
result.append(c);
} else {
if (capitalizeNext) {
result.append(Character.toUpperCase(c));
} else {
result.append(Character.toLowerCase(c));
}
capitalizeNext = false;
}
}
return result.toString();
}
/**
* <p>Encodes the source {@code String} to the specified encoding.</p>
*