6 Replies Latest reply on Oct 18, 2014 6:58 AM by Obi-wan Kenobi

    Spilt a name which has more than one space

    LearnIndesignScripting

      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;

        • 1. Re: Spilt a name which has more than one space
          cchimi Level 2

          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.

          • 2. Re: Spilt a name which has more than one space
            Kai Rübsamen Adobe Community Professional

            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 );
            
            • 3. Re: Spilt a name which has more than one space
              Peter Kahrel Adobe Community Professional & MVP

              Or this:

               

              var name="Alexandra De Poorter";
              var parts = name.match(/(.+?) (.+)/);
              var finalname = parts[2] + ', ' + parts[1];
              
              

               

              Peter

              • 4. Re: Spilt a name which has more than one space
                Obi-wan Kenobi Adobe Community Professional

                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!

                • 5. Re: Re: Spilt a name which has more than one space
                  Peter Kahrel Adobe Community Professional & MVP

                  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

                  • 6. Re: Re: Spilt a name which has more than one space
                    Obi-wan Kenobi Adobe Community Professional

                    Thanks Peter! And my greetings to your niece!