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

Validation for capitalizeFirstChar #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

pvs1209pvs
Copy link
Contributor

Return the original string if the input is a blank string or empty.

@pvs1209pvs pvs1209pvs closed this Jun 28, 2023
@pvs1209pvs pvs1209pvs reopened this Jun 28, 2023
// to avoid repeated trimming
val inputTrimmed = this.trim()

if (inputTrimmed.isBlank() || inputTrimmed.isEmpty()) return this
Copy link
Owner

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())
Copy link
Owner

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?

  1. Aa Bb Cc Dd (with trim, removing extra spaces)
  2.  Aa Bb Cc Dd  (without trim, removing extra spaces)
  3. Aa Bb Cc Dd (with trim, preserving extra spaces)
  4.  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.

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants