Added capitalizeWords renderer
This commit is contained in:
parent
daadd0bc4a
commit
99f6afe4fb
4 changed files with 103 additions and 9 deletions
|
@ -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>
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue