Separate a Full Name Into First, Middle, and Last Names

From HotDocs Wiki

Jump to: navigation, search

If you use a variable in your template for an individual's full name, you may run into situations where you would like to parse (break up) the full name into its individual parts. For example, you may want to alphabetize a list of names by last name, in which case you would need to extract the last names from the full name.

NOTE:

In general, using separate variables for first, middle, and last names in the first place (instead of parsing the full name into individual parts) gives you more flexibility.

You can use the following computation to parse a full name into first, middle, and last names. If the full name does not contain any spaces, the computation assumes that it is only the first name. If there is only one space, the computation assumes that there is no middle name. Finally, if there are more than two spaces in the name, all characters after the first two spaces are considered part of the last name.

NOTE:

This computation assumes that the full name includes a single space between the first and middle names and the middle and last names. To automatically eliminate duplicate spaces between names before parsing it, see Remove Extra Spaces From an Answer.

Example

""
Full Name 
SET Num Var TO  POSITION( RESULT, " " ) 
IF Num Var > 0 
  SET First Name TO FIRST( RESULT, Num Var - 1 ) 
  MID( RESULT, Num Var + 1, LENGTH( RESULT ) - Num Var ) 
  SET Num Var TO POSITION( RESULT, " " ) 
  IF Num Var > 0 
    SET Middle Name TO FIRST( RESULT, Num Var - 1 ) 
    SET Last Name TO MID( RESULT, Num Var + 1, LENGTH( RESULT ) - Num Var ) 
  ELSE
    SET Middle Name TO "" 
    SET Last Name TO RESULT 
  END IF  
ELSE  
  SET First Name TO RESULT 
  SET Middle Name TO "" 
  SET Last Name TO "" 
END IF 
""

Full Name A Text variable used for an individual's full name (first, middle, and last names).
First Name A temporary Text variable used to store the individual's first name. (See Create a Temporary Variable.)
Middle Name A temporary Text variable used to store the individual's middle name.
Last Name A temporary Text variable used to store the individual's last name.
Num Var A temporary Number variable used to track the position of spaces in the name.