简体   繁体   中英

Is it possible to convert full name to firstname and lastname?

I need to convert full name to first and last name. Is this possible within ~90% success rate if names are mostly western?

You assume that "western names" all have the same simple format. That is not so. For example, Spanish names generally have two "last names", but can get quite a bit more complicated.

Many European countries have nobility particles that can complicate names.

Names are cultural, and cultures are much more diverse and complex than most people imagine. Even in "the West".

I don't think so. It mostly depends on the data that you have available. If the user always enters "Firstname Lastname" then you can check for the last whitespace, perform a separation and that's it.

But for the typical German customer there are a wide range of possible missing matches. Names like "Hans Ulrich-Schmidt" where the user forgets the dash (or even worse, explicitely doesn't enter it) will not be parsed correctly. But there is no clear way to determine whether the "Ulrich" in "Hans Ulrich Schmidt" ist part of the first or the last name.

That's just one example, there are many more so I think: No, it's not possible.

Assuming that your input is FirstName Name Name LastName, split the string and use first and last indexes:

String bigName = "John McDonalds Harris"
String[] names = bigName.split(" ");
System.out.println(String.format("FirstName: %s : LastName: %s", names[0], names[names.length-1])); 

It will print : FistName: John : LastName: Harris

It depends on the initial format. For example in VCards the standard format is

LastName;FirstName

The delimiter is semicolon.

In general case it is extremely hard. I agree with guys that already said this. If you seriously want to solve the problem you have to implement Locale dependent parser. For example English are usually composed as first name and then last name. Hungarians write last name and then first names. In Russian it is more complicated: sometimes it is first name and last name, sometimes they use reverse order. It depends on the text style. The reverse order is used in more formal texts.

You can try to use dictionaries of first and last names. This can help in some cases. But what to do with name like "Elton John"? And what about "Warren Christopher"? and "Christopher Robin"?

Probably if you have a large list of names and all names are written using the same format you can first detect the style using names dictionary and then use it. You will probably get 90% of success.

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM