如何全名字符串分成名字和姓氏字符串?

编程入门 行业动态 更新时间:2024-10-18 16:42:11
本文介绍了如何全名字符串分成名字和姓氏字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一些帮助,这一点,我有一个全名字符串,我需要做的是独立的,用这个全名字符串作为名字和姓氏分别。

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.

推荐答案

这会如果你确信你有一个名字和姓氏工作。

This will work if you are sure you have a first name and a last name.

string fullName = "Adrian Rules"; var names = fullName.Split(' '); string firstName = names[0]; string lastName = names[1];

请确保您检查名称的长度。

names.Length == 0 //will not happen, even for empty string names.Length == 1 //only first name provided (or blank) names.Length == 2 //first and last names provided names.Length > 2 //first item is the first name. last item is the last name. Everything else are middle names

更新

当然,这是问题的一个相当简化的视图。我的答案的目的是介绍如何 string.Split()的作品。但是,你必须记住,有些姓氏是复合的名称,如路易斯·席尔瓦,如@AlbertEin指出。

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.

更多推荐

如何全名字符串分成名字和姓氏字符串?

本文发布于:2023-11-08 20:21:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   全名   姓氏   名字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!