-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Validation for capitalizeFirstChar #14
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,16 +49,24 @@ inline fun String.replace(ignoreCase: Boolean = false, vararg vars: Pair<String, | |
inline fun String.remove(substring: String) = replace(substring, "") | ||
|
||
/** | ||
* Capitalizes first character of every word. | ||
* Words are separated by one of more space characters. | ||
* Capitalized the first character of every word where word is delimited by a space character. | ||
*/ | ||
inline fun String.capitalizeFirstChar() = | ||
lowercase(Locale.getDefault()) | ||
.split(" +".toRegex()) | ||
fun String.capitalizeFirstChar(): String { | ||
|
||
// to avoid repeated trimming | ||
val inputTrimmed = this.trim() | ||
|
||
if (inputTrimmed.isBlank() || inputTrimmed.isEmpty()) return this | ||
|
||
return inputTrimmed | ||
.lowercase(Locale.getDefault()) | ||
.split(" ") | ||
.joinToString(" ", "") { d -> | ||
d.replaceRange(0, 1, d.first().uppercaseChar().toString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trim only removes prefix and suffix spaces, so changing the split " aa bb cc dd ".capitalizeFirstChar()
// NoSuchElementException: Char sequence is empty. Error is in the What should be the correct output?
For the 4th output (preserving everything in the original string but with the first char of every word to uppercase which is the purpose of this method), this would do it: fun String.capitalizeFirstChar(): String {
if (isBlank()) return this
return lowercase(Locale.getDefault())
.split(" ")
.joinToString(" ") { d ->
if (d.isEmpty()) d else d.replaceRange(0, 1, d.first().uppercaseChar().toString())
}
} Also the Changing the split to the previous Trimming I think should be done outside this method so the intentions of the code are more clear.
This comment was marked as outdated.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have made the following changes
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Removes non-numerical characters from a string. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if input is trimmed then
isEmpty
is equivalent toisBlank
, soisEmpty
is enough