How to separate first and last name in C #?

I am looking for how to get rid of an exception. "The index was outside the array." for case 2 below

Purpose: to separate the first and last name (last name can be several times)

Case 1:

Name: John Melvick

I can resolve the first case with my code

Case 2:

Name: Kennedy

In case of two, I get an error. The index was out of range of LastName in my code

Case 3:

Name: Rudolph Nick Bother

In case 3, I can get:

FirstName: Rudolph and LastName: Nick (while I need Nick Bour to be last together)

Very grateful if anyone would help me.

Here is the code:

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];
}
+4
source share
6 answers

. - :

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

, :

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

- :

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 : "" ;

, . , ( ):

  • ( " " )

, . " " , , "".

, , (), (). , , , -. , . Mountbatten-Windsor , , - " ".

+1
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

. .

0

String.indexof(" ")

string.lastindexof(" ")

, . 2. , 0, . ,

, , , ,

0

Change the code like this:

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 + " ";
    }
  }
} 
0
source

Here is the most general solution to this problem.

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;

   }
0
source

All Articles