How to split use full name to first name and last name Android Kotlin?

Ansar Ali
Nov 5, 2020

--

Let’s define it.

  • Create a variable to store user full name .
  • And store call lastIndexof fun in a different variable with user full name
  • After that define user first name and last name with substring in defferent defferent variables.
  • Last call these variables in XML ids.

Our code should.

Val fullName = “Ansar Ali”

Val index = fullName?. lastIndexof (‘ ’)

val firstName = index?.let { it1 -> fullName?.substring(0, it1) }
val lastName = index?.plus(1)?.let { it1 -> fullName?.substring(it1) }
user_firstName.setText(firstName)
user_lastName.setText(lastName)

Hope it’s helpful.

Thank you

--

--

Ansar Ali
Ansar Ali

Written by Ansar Ali

Flutter and Android app developer.

Responses (1)