03Jan

jQuery split string at the last space in variable

Quick one about splitting a variable in two from last chosen character. I use it from a name string with first name(s) and surname.

In my case, I wanted to get the last element on a new line to follow the design. 

Split variable from last space in a string

var fullname = person.Name; // object format: first name(s) last name

var fname = fullname.substring(0, fullname.lastIndexOf(” “) + 1);
var lname = fullname.substring(fullname.lastIndexOf(” “) + 1, fullname.length);

Hope this can help someone.

1
Leave a Reply

1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
  Subscribe  
newest oldest
Notify of
Konrad Pietrzykowski

Thanks mate! Good example! :)