Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to Split Full Name

I am looking for a Regex to be using in my Java app to split Full Name into First & Last Name parts.

The Full Name will always be separated by " " (Space as Delimiter). some name can consist of Middle Name , But they can be combined with FirstName , i only want to separate Last Name as separate group.

For example :
"This is My Fullname"
 LastName = Fullname
 FirstName = This is My

So the logic being , anything after Last WhiteSpace is considered as LastName and everything before that as FirstName.

like image 355
Ashesh Nair Avatar asked May 26 '26 13:05

Ashesh Nair


1 Answers

It seemed to me that regular expressions were not really best for this case.

String fullName = "This is My Fullname";
int index = fullName.lastIndexOf(' ');
String lastName = fullName.substring(index+1);
String firstName = fullName.substring(0, index);
like image 86
WillShackleford Avatar answered May 30 '26 04:05

WillShackleford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!