Reputation: 71
I have this var fullName = 'Muhammad Ali'
, what I want to do is split the First Name and Last name from it.
Upvotes: 0
Views: 1138
Reputation: 1451
Using a library like parse-full-name would usually get you a better result than using a simple split on strings.
The string may have titles inside or other inconsistencies that a library would hopefully parse better.
Upvotes: 0
Reputation: 1456
You can use the .split() method to split the string to an array based on spaces in the string. I would use caution with this as names can differ in length and have more than one space.
Upvotes: 0
Reputation: 631
For simple cases, use .split()
. Usually a simple google search can help you.
let fullName = 'Muhammad Ali'
let formattedName = fullName.split(" ")
console.log(formattedName)
Upvotes: 3