Split First name and Last name using javascript

后端 未结 20 3173
悲&欢浪女
悲&欢浪女 2020-12-08 06:39

I have a user with a 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 lastname fro

20条回答
  •  天命终不由人
    2020-12-08 06:52

    if you assume the last word is the last name and a single word name is also a last name then ...

    var items = theName.split(' '),
        lastName = items[items.length-1],
        firstName = "";
    
    for (var i = 0; i < items.length - 1; i++) {
       if (i > 0) {
          firstName += ' ';
       }
       firstName += items[i];
    }
    

提交回复
热议问题