gpt4 book ai didi

java - 正则表达式拆分全名

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:43:07 25 4
gpt4 key购买 nike

我正在寻找要在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com