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
Open
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.

}

}

/**
* Removes non-numerical characters from a string.
*/
Expand Down