Skip to main content
October 16, 2014
Question

Spilt a name which has more than one space

  • October 16, 2014
  • 3 replies
  • 595 views

I have a name as :  ashish Kumar Panday

I just want first name into one sring and rest into other as :  F Name=ashish

lName = Kumar Panday

I am trying with this code but it not working

var name="Alexandra De Poorter";

var splitName=name.toString().indexOf (' ');

var aspiltName=name.split (9);

var fname=aspiltName[0];

var LName=aspiltName[1];+ ",";

//~ var LName=name.toString().split (splitName,name.length) + ",";

var finalname=LName + fname;

This topic has been closed for replies.

3 replies

Peter Kahrel
Community Expert
Community Expert
October 18, 2014

Or this:

var name="Alexandra De Poorter";

var parts = name.match(/(.+?) (.+)/);

var finalname = parts[2] + ', ' + parts[1];

Peter

Obi-wan Kenobi
Legend
October 18, 2014

Hi all,

A little question, by curiosity:


How manage these cases:


John F. Kennedy

John Fitzgerald Kennedy

Eddy van Halen

Louis Antoine de Saint-Just

Charles de Gaulle

… to obtain:

Kennedy, John F.

Kennedy, John Fitzgerald

van Halen, Eddy

Saint-Just (Louis Antoine de)

de Gaulle (Charles)

Thanks in advance!

Peter Kahrel
Community Expert
Community Expert
October 18, 2014

Matching names is not so easy, especially because you won't be able to tell the difference between Portuguese and Spanish names such as my niece's name, Lara David Kahrel (who lives in Portugal), whose last name is 'David Kahrel', and John Fitzgerald Kennedy, whose last name is 'Kennedy'. That aside, your examples (and more) can be captured as follows:

var parts = name.match(/^(.+?) ((?:v[ao]n )?(?:d[eu]r? ?)?(?:la ?)?)?([^ ]+)$/i);

var finalname = "";

if (parts[2] != "") finalname = parts[2]; // if there are no prefixes, as in 'John Black', then parts[2] == ""

finalname += parts[3] + ', ' + parts[1];

The expression chops strings up in two or three parts: last name (everything from the last space to the end of the string), prefixes (any combination of 'van', 'von', 'van de', etc.), and first name (everything up to the first prefix or up to the last name. In this form it captures these (Dutch, Flemish, German, French) prefixes case-insensitively:

van/von

de

van/von der

van de

de

de la

le

la

You could add others, such as 't, op, della, etc. etc.

Peter

Kai Rübsamen
Participating Frequently
October 16, 2014

Try this one:

var name = "Alexandra De Poorter";

var fName = name.slice( 0 , name.indexOf(" ") );

var lName = name.slice( name.indexOf(" ") + 1 );

alert ( fName + "\r" + lName + "\r" + fName + " "+ lName );

Inspiring
October 16, 2014

One relatively simple way is to split the string on the space, creating an array of each element. Take the first element for the first name, and join the rest of the array back together to form the last name.

var name="Alexandra De Poorter";

var splitName = name.split(" ");

var firstName = splitName.shift();

var lastName = splitName.join(" ");

var finalname = lastName + ", " + firstName;

$.writeln(finalname);

Note that shift() actually removes the first item from the array so you can simply join the rest together without worrying about the first name.