Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringCleanup - Allow to capitalize strings #523

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/sirius/kernel/commons/StringCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,20 @@ public static String uppercase(@Nonnull String input) {
return input.toUpperCase();
}

/**
* Capitalizes the first character of the given string.
*
* @param input the input to process
* @return the resulting string
*/
public static String capitalize(@Nonnull String input) {
char titleCasedChar = Character.toTitleCase(input.charAt(0));
if (titleCasedChar == input.charAt(0)) {
return input;
}
return titleCasedChar + input.substring(1);
}

/**
* Removes all {@linkplain #PATTERN_STRIP_XML XML tags} from the given string.
*
Expand Down
12 changes: 12 additions & 0 deletions src/test/kotlin/sirius/kernel/commons/StringsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ class StringsTest {
UnaryOperator { term: String? -> StringCleanup.reduceCharacters(term) },
UnaryOperator { input: String? -> StringCleanup.uppercase(input!!) })
)
assertEquals(
"Hello",
Strings.cleanup("hello", UnaryOperator { input: String? -> StringCleanup.capitalize(input!!) })
)
assertEquals(
"HeLLo",
Strings.cleanup("heLLo", UnaryOperator { input: String? -> StringCleanup.capitalize(input!!) })
)
assertEquals(
"-hello-",
Strings.cleanup("-hello-", UnaryOperator { input: String? -> StringCleanup.capitalize(input!!) })
)
assertEquals(
"Hello",
Strings.cleanup("Hel-lo", UnaryOperator { input: String? -> StringCleanup.removePunctuation(input!!) })
Expand Down