开发者

Parse full name into First Name and Last Name from SQL Server - Linq to Entities

I have a C# console application which simply pulls information from a Sql Server 2008 d开发者_StackOverflow社区atabase and outputs it to an excel sheet.

I am using Linq to Entities.

Here is a snippet of the code just to demonstrate how I output the data from the database to the excel sheet.

cell = dataRow.CreateCell(colPosition++); // Title Group ID
cell.SetCellValue(currentTitle.TitleGroupID);

My issue is, that in my database, the Users name is stored as his/her full name, e.g. "John Smith", however, in the excel sheet this is separated into First Name and Last Name.

Is there a way to parse the users name into First Name and Last Name?

Thanks in advance, if more info is needed, just ask :)


if you are sure the first name doesn't contain any spaces you can use the string.Split method

string [] Name = fullNameString.Split(" ");
string firstname = Name[0];
string lastname = Name[1];


The thing is with name is that they are complicated. So for short, no you can't do that since it is basically impossible to know what is a last name and first name from a full name. But you can go with the assumptions that the last part is always the last name (even though that is not the case in many situations) and split the string on space.

string [] Name = fullNameString.Split(" ");
string firstname = string.Join(" ", Name.Take(Name.Length - 1));
string lastname = Name.Last();


You could split the string up by spaces, and take the first word as the first name, and the remaining words as the surname.


You can use the naive approach of simply splitting the last word off.

However this isn't something that can be reliably solved without knowing more about language. "Fred Di Costello" actually has the lastname "Di Costello", same with "Kraus Von Trappe".


it is very difficult task. You should to analyze all your data. for simple case you can split text by " " and first name set first word, last name set second world. But if you have other case you should resolve it with other way

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜