Splitting a fullname into firstname, lastname

4
I am trying to split a Full Name ("Robert van 't Hof") into First Name ("Robert"), Middle Name ("van 't") and Last Name ("Hof"). I've been reading the Expressions page but I'm not quite sure how to use these to get a good result. Especially considering some people don't have a Middle Name and I don't want it to use their Last Name as a Middle Name just because that's the second part of their name. Edit: Any help on how to do this in a Java action?
asked
2 answers
7

Pfff, I think you will have to resort to Java for this, there are substringfunctions in Microflow that will allow you to do 'substring(Robert, 4)' which means that it will take 4 characters of the string resulting in 'Robe' in this case.

This will not do the trick for you I'm afraid, as names vary in representation and number of characters. With Java, you will be able to 'detect' the spaces in the fullname and split up 'Robert van't Hof' into 'Robert' 'van' 't' 'Hof' (you will need to think of what you want to do with you middlename however, as it contains a space itself)

answered
4

How to do this in Java is not really a Mendix related question, however you can do this with the String split method. See http://www.j2ee.me/j2se/1.4.2/docs/api/java/lang/String.html#split%28java.lang.String%29 for more information. Specify a space as the method parameter and you will have the name split in a String array consisting of "Robert", "van" "'t" and "Hof".

After that it's up to you what to do with this data, you could simply pick the first substring as the first name and the last substring as the last name and treat everything in between as middle name. However you will get exceptions to this case (consider "Jan Pieter" as a first name for example) so the only way to make sure is by getting the different parts of the name as individual fields.

answered