我正在寻找要在我的 Java 应用程序中使用的正则表达式,以将全名拆分为名字和姓氏部分。
全名将始终由“”(空格作为分隔符)分隔。 有些名字可以由 Middle Name 组成,但它们可以与 FirstName 组合,我只想将 Last Name 分开作为单独的组。
For example :
"This is My Fullname"
LastName = Fullname
FirstName = This is My
逻辑是,Last WhiteSpace 之后的所有内容都被视为 LastName,之前的所有内容都被视为 FirstName。
最佳答案
在我看来,正则表达式并不是最适合这种情况的。
String fullName = "This is My Fullname";
int index = fullName.lastIndexOf(' ');
String lastName = fullName.substring(index+1);
String firstName = fullName.substring(0, index);
关于java - 正则表达式拆分全名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31800033/