Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  90] [ 6]  / answers: 1 / hits: 161445  / 12 Years ago, sun, september 9, 2012, 12:00:00

I have a user with the name Paul Steve Panakkal. It's a long name it won't fit to the div container. So is there anyway to split first name and last name from it using JavaScript or jQuery?


The name is got from PHP into a variable in JavaScript. This is then splitted using JS.


More From » string

 Answers
140

You should use the String.prototype.split() method:



'Paul Steve Panakkal'.split(' '); // returns [Paul, Steve, Panakkal]


You can use it this way:



'Paul Steve Panakkal'.split(' ').slice(0, -1).join(' '); // returns Paul Steve
'Paul Steve Panakkal'.split(' ').slice(-1).join(' '); // returns Panakkal


So in common:



var firstName = fullName.split(' ').slice(0, -1).join(' ');
var lastName = fullName.split(' ').slice(-1).join(' ');

[#83168] Friday, September 7, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ingridcassieb

Total Points: 346
Total Questions: 97
Total Answers: 125

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
;