如何将全名字符串分成名字和姓氏字符串?
我需要一些帮助,我有一个全名字符串,我需要做的是单独使用这个全名字符串作为名字和姓氏。
I need some help with this, I have a fullname string and what I need to do is separate and use this fullname string as firstname and lastname separately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
这是我的扩展。它返回最后一个单词作为姓氏,其余单词作为名字。
用法:
顺便说一句,Visual Studio Intellisense 正在失控!我只编写了
if
条件的代码,其余的由 Intellisense 完成。Here is my extension. It returns the last word as the last name and the rest as the first name.
Usage:
On a side note Visual Studio Intellisense is getting out of hand! I only wrote the code up to the
if
condition and the rest was completed by Intellisense.dart/flutter 中的解决方案:
Solution in dart/flutter:
如果您确定自己有名字和姓氏,这将起作用。
确保检查
名称
的长度。更新
当然,这是对问题的相当简化的看法。我的回答的目的是解释 string.Split() 的工作原理。但是,您必须记住,有些姓氏是复合名,例如 @AlbertEin 指出的“Luis da Silva”。
这远不是一个简单就能解决的问题。一些介词(法语、西班牙语、葡萄牙语等)是姓氏的一部分。这就是为什么@John Saunders 问“什么语言?”。 John 还指出,前缀(Mr.、Mrs.)和后缀(Jr.、III、MD)可能会造成妨碍。
This will work if you are sure you have a first name and a last name.
Make sure you check for the length of
names
.Update
Of course, this is a rather simplified view on the problem. The objective of my answer is to explain how
string.Split()
works. However, you must keep in mind that some last names are composite names, like "Luis da Silva", as noted by @AlbertEin.This is far from being a simple problem to solve. Some prepositions (in french, spanish, portuguese, etc.) are part of the last name. That's why @John Saunders asked "what language?". John also noted that prefixes (Mr., Mrs.) and suffixes (Jr., III, M.D.) might get in the way.
您可以尝试使用空格来解析它,但它不会起作用,示例:
但是如果有大量用户输入,这会失败,如果他确实有两个名字怎么办? “胡安·巴勃罗·佩雷斯”。
名称很复杂,因此不可能始终知道给定字符串中名字和姓氏的哪一部分。
编辑
您不应该使用 string.Split 方法来提取姓氏,有些姓氏是由两个或多个单词组成的,例如我的一个朋友的姓氏是“Ponce de Leon”。
You could try to parse it using spaces but it's not going to work, Example:
But that would fail with a ton of user input, what about if he does have two names? "Juan Pablo Perez".
Names are complicated things, so, it's not possible to always know what part is the first and last name in a given string.
EDIT
You should not use string.Split method to extract the last name, some last names are composed from two or more words, as example, a friend of mine's last name is "Ponce de Leon".
此解决方案适用于姓氏包含多个单词的人。将第一个单词视为名字,将其他所有内容视为姓氏。
例如,将 Brian De Palma 传递到上述函数中将返回“De Palma, Brian”
This solution will work for people that have a last name that has more than one word. Treat the first word as the first name and leave everything else as the last name.
For Example passing Brian De Palma into the above function will return "De Palma, Brian"
您可以使用此版本 (MSDN)<
Split
方法的 /strong> 如下:在这种情况下,
split[0]
将是John
和split[ 1]
将是Deo
。另一个例子:在这种情况下,
split[0]
将是John
,split[1]
将是Wesley Doe
。请注意,
split
的长度永远不会超过 2因此,通过以下代码,您可以很好地获得
FirstName
和LastName
:希望这个答案为本页面添加一些有用的内容。
You can use this version (MSDN) of
Split
method like follow:In this case
split[0]
will beJohn
andsplit[1]
will beDeo
. another example:In this case
split[0]
will beJohn
andsplit[1]
will beWesley Doe
.Notice that the length of
split
never be more than 2So with following code you can get
FirstName
andLastName
nicely:Hope this answer add something useful to this page.
尝试:
Try:
我建议使用 Regex 来严格定义您的名字和姓氏。
I would recommend using a Regex to rigorously define what your first and last names look like.
nuget 有多种名称解析/拆分的实现。如果您深入研究 NameParserSharp 存储库,您还可以组合两个部分类并复制并复制它。粘贴到您自己的库中。
NameParserSharp
更多内容请参见 Nuget
There are several implementations of name parsing/splitting over at nuget. If you dive into the NameParserSharp repository you can also just combine two partial classes and copy & paste into your own library.
NameParserSharp
More at Nuget
name =“托尼·斯塔克死了”;
name ="Tony Stark is dead";
这是我在项目中使用的一段 C# 代码。它返回最后一个单词作为姓氏,其余单词作为名字。
Fiddle
输出:
PS 抱歉,没有中间名。
Here is a piece of c# code that I use on my projects. It returns the last word as surname and the rest as name.
Fiddle
Output:
P.S. No middle name, sorry.
这就像调用 string.Split() 一样简单吗,传递一个空格作为分割字符?或者这里发生了什么更棘手的事情? (如果是后者,请用更多信息更新您的问题。)
Is this as simple as calling string.Split(), passing a single space as the split character? Or is there something trickier going on here? (If the latter, please update your question with more info.)
对于基本用例,很容易将其拆分为“ ”或“,”,但是由于名称多种多样,其中包含不同的内容,这并不总是有效。
for basic use cases its easy to just split on ' ' or ', ' however due to the variety of names with differing things in them this is not going to always work.
您可以尝试移植此 PHP 库 https://github.com /joshfraser/PHP-Name-Parser/blob/master/parser.php
You can try to port this PHP lib https://github.com/joshfraser/PHP-Name-Parser/blob/master/parser.php
所以如果你把第一个空格作为名字,其余的作为姓氏,这会给我们
So if you take the First space as Name and rest as Surname, this would give us
简单的代码可以将Flowers, Love等内容转换为Love Flowers(这适用于非常复杂的名称,例如Williams Jones、Rupert John) :
输出:爱花
相反。爱花转换为花,爱:
Easy, simple code to transform something like Flowers, Love to Love Flowers (this works with very complex names such as Williams Jones, Rupert John):
output: Love Flowers
The other way around. Love Flowers converted to Flowers, Love:
有不止一种方法可以实现这一点。我的具体情况通过如下代码示例解决。
例如
There is more than one method for this. My specific situation is solved with a code example like below.
for Example
您可以创建一个值对象来表示名称并在应用程序中使用它
:
you can create a value object to represent a name and use it in your application
Usages:
迟到总比不到好,这是我在许多应用程序中使用的解决方案。
享受 !
Better to be late than never, here is my solution for this used in many applications.
Enjoy !
我添加了更多检查以获得所需的结果
例如,其他解决方案有效,但当我故意占用两个空格时,它不起作用,所以这里是解决方法。
结果
I've added some more check to get the desired result
for example other solution work but when i intentionally take two spaces it do not work, so here is workaround.
Result