Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Spilt a name which has more than one space

Guest
Oct 16, 2014 Oct 16, 2014

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;

TOPICS
Scripting
575
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 16, 2014 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 16, 2014 Oct 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 );

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 18, 2014 Oct 18, 2014

Or this:

var name="Alexandra De Poorter";

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

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

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 18, 2014 Oct 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 18, 2014 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 18, 2014 Oct 18, 2014
LATEST

Thanks Peter! And my greetings to your niece! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines