簡體   English   中英

如何在c#中分隔名字和姓氏?

[英]How to separate a first name and last name in c#?

我正在尋找如何擺脫異常“索引超出數組的范圍。” 對於以下案例2

目的:分隔名字和姓氏(姓氏有時可能為空)

情況1:

姓名:John Melwick

我可以用我的代碼解決第一個案例

案例2:

姓名:肯尼迪

如果兩個我收到錯誤索引超出了我的代碼中LastName的范圍

案例3:

姓名:Rudolph Nick Bother

在案例3中,我可以得到:

名字:魯道夫和姓氏:尼克(而我需要尼克打擾一起作為姓)

非常感謝,如果有人幫助我的話。

這是代碼:

Match Names = Regex.Match(item[2], @"(((?<=Name:(\s)))(.{0,60})|((?<=Name:))(.{0,60}))", RegexOptions.IgnoreCase);

if (Names.Success)
{
    FirstName = Names.ToString().Trim().Split(' ')[0];                      
    LastName = Names.ToString().Trim().Split(' ')[1];
}

拆分字符串,並限制要返回的子字符串數。 這將保留第一個空格后的任何內容作為姓氏:

string[] names = Names.ToString().Trim().Split(new char[]{' '}, 2);

然后檢查數組的長度以處理只有lastname的情況:

if (names.Length == 1) {
  FirstName = "";
  LastName = names[0];
} else {
  FirstName = names[0];
  LastName = names[1];
}

像這樣的東西有效:

string name = "Mary Kay Jones" ;
Regex rxName = new Regex( @"^\s*(?<givenName>[^\s]*)(\s+(?<surname>.*))?\s*$") ;
Match m = rxName.Match( name ) ;

string givenName = m.Success ? m.Groups[ "givenName" ].Value : "" ;
string surname   = m.Success ? m.Groups[ "surname"   ].Value : "" ;

但這是一個極其錯誤的假設,即給定的名稱只包含一個單詞。 我可以想到許多相反的例子,例如(但絕不限於):

  • Billy Ray(如早期的“Billy Ray Cyrus”中的例子)
  • 玫琳凱
  • 瑪麗貝絲

如果不問問有關人員,就沒有真正的方法可以知道。 “瑪麗貝絲瓊斯”是由一個給定的,中間的和姓氏組成的,或者由一個給定的名字組成,瑪麗貝絲和一個姓氏“瓊斯”。

如果你正在考慮講英語的文化,通常的慣例是,人們可以擁有盡可能多的名字(姓名),后跟姓氏(姓氏)。 例如,英國皇冠的繼承人查爾斯王子帶着相當沉重的查爾斯菲利普亞瑟·喬治·蒙巴頓 - 溫莎。 嚴格來說,他沒有姓氏。 當需要一個人使用Mountbatten-Windsor時,他的全名只是“Charles Phillip Arthur George”。

string fullName = "John Doe";
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.indexof(" ")

string.lastindexof(" ")

如果匹配則有一個空格。 如果他們不存在2.我認為如果沒有匹配則返回0。 希望這可以幫助

編輯

如果您使用索引,則可以使用它們執行子字符串並獲取您想要的姓氏

將代碼修改為:

Match Names = Regex.Match(item[2], @"(((?<=Name:(\s)))(.{0,60})|((?<=Name:))(.{0,60}))", RegexOptions.IgnoreCase);

if (Names.Success)
{
  String[] nameParts =  Names.ToString().Trim().Split(' ');
  int count = 0;
  foreach (String part in nameParts) {
    if(count == 0) {
      FirstName = part;
      count++;
    } else {
      LastName += part + " ";
    }
  }
} 

以下是此問題的最通用解決方案。

public class NameWrapper
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public NameWrapper()
    {
        this.FirstName = "";
        this.LastName = "";
    }
}
 public static NameWrapper SplitName(string inputStr, char splitChar)
   {
       NameWrapper w = new NameWrapper();
       string[] strArray = inputStr.Trim().Split(splitChar);
       if (string.IsNullOrEmpty(inputStr)){
           return w;
       }


       for (int i = 0; i < strArray.Length; i++)
       {
           if (i == 0)
           {
               w.FirstName = strArray[i];
           }
           else
           {
               w.LastName += strArray[i] + " ";
           }
       }
       w.LastName = w.LastName.Trim();

       return w;

   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM