iuni
iuni

Reputation: 23

Kotlin - Handling Names From EditText

I have two EditTexts which take in a input type of android:inputType="textPersonName|textCapWords"

The user will presumably input their name ("John Doe"), and this will be saved unto Firestore.

I believe that textPersonName|textCapWords will handle capitalizing the first characters in their names, however, I want to handle a case in which a user puts one too many empty spaces between their name ("John Doe").

I've thought of doing something like this:

val splitCMName = caseManager.split(" ")

    val joinedCMName = splitCMName.joinToString {
        splitCMName[0] + " " + splitCMName[splitCMName.lastIndex]

    })

However, I'm wondering how to implement this in a more Kotlin/Android Studio manner, if there is one. I think my approach to this might be too Pythonic.

I would highly appreciate any suggestions. Thank you.

Upvotes: 0

Views: 124

Answers (1)

Ben P.
Ben P.

Reputation: 54224

How about using regular expressions to replace all whitespace with a single space character?

val normalized = caseManager.replace(Regex("\\s+"), " ")

Upvotes: 1

Related Questions