本文介绍了如何全名字符串分成名字和姓氏字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于如何全名字符串分成名字和姓氏字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 20:01