-
-
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?
Conversation
// to avoid repeated trimming | ||
val inputTrimmed = this.trim() | ||
|
||
if (inputTrimmed.isBlank() || inputTrimmed.isEmpty()) return this |
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 to isBlank
, so isEmpty
is enough
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Trim only removes prefix and suffix spaces, so changing the split " +".toRegex()
to " "
this line gives error when there is more than one space between characters, for instance:
" aa bb cc dd ".capitalizeFirstChar()
// NoSuchElementException: Char sequence is empty.
Error is in the first
method with an empty split, although when using anything else like d.replaceRange(0, 1, "A") the replaceRange
method also fails with: IndexOutOfBoundsException: start 1, end 0, length 0
What should be the correct output?
Aa Bb Cc Dd
(with trim, removing extra spaces)Aa Bb Cc Dd
(without trim, removing extra spaces)Aa Bb Cc Dd
(with trim, preserving extra spaces)Aa Bb Cc Dd
(without trim, preserving extra spaces)
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 replaceRange
and toString
can be changed to d.first().uppercaseChar() + d.substring(1)
, whatever you prefer.
Changing the split to the previous " +".toRegex()
then the output would be the 2nd.
Trimming I think should be done outside this method so the intentions of the code are more clear.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
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.
I have made the following changes
- Caller is responsible for trimming the string.
- Caller and choose to preserve the spacing.
- Exception is thrown for empty and blank strings.
- Appropriate tests are written.
…orws exception for illegal string
Return the original string if the input is a blank string or empty.