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.

Subscribe
Notify of

1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Konrad Pietrzykowski
3 years ago

Thanks mate! Good example! :)