1

I'm having trouble with this program. The goal is to parse a name(as a string) using string methods. I'm only able to get the first and last name however I'm having trouble with prefixes. This is what I have so far any help is appreciated!

4
  • Add an example of what you want from a sample name string. Commented Sep 15, 2015 at 4:16
  • Can you provide an example input of "textBox1.Text"? The way you are trying to approach this is extremely hacky and really dependant on the input being in a really specific format. Commented Sep 15, 2015 at 4:16
  • @ArghyaC ah sorry, the prefix, first, and last name are supposed to be output on three different labels. so if someone were to put Mr. Doug Socks, label 1 would = Mr., label 2 = Doug, and label 3 = Socks Commented Sep 15, 2015 at 4:18
  • 2
    My most honest suggestion would be to capture the info as three separate textboxes. In my case, I would type "Ing. Alejandro Ariel González Cimé", not fitting your "rules" Commented Sep 15, 2015 at 4:30

4 Answers 4

2

What I understand is that a full name can contains Initials, FirstName, MiddleName and Last Name. There would be a few combinations which can be taken care while fetching the spiliting the name from full name.

First Case

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

Second Case

string fullName = "Mr. Mohit Shrivastava";
var names = fullName.Split(' ');
string prefix= names[0];
string firstName = names[1];
string lastName = names[2];

Third Case

string fullName = "Mr. Mohit Kumar Shrivastava";
var names = fullName.Split(' ');
string prefix= names[0];
string firstName = names[1];
string middleName = names[2];
string lastName = names[3];

in your case Case Second will serve your need. If you are not sure what the user is going to put as his/her name than probably you can count the count the item in names array and see if which case fits for you.

Sign up to request clarification or add additional context in comments.

1 Comment

I haven't learned the split function yet, but this works. I'm not allowed to use this but thanks!
0

This will do the trick

var parts = fullName.Replace("  ", " ").Trim().Split(' ');
lastName = parts[parts.Length - 1];
if (parts.Length >= 2)
   firstName = parts[parts.Length - 2];
if (parts.Length >= 3)
   prefix = parts[parts.Length - 3];

Comments

0

If you have to make the assumption that a name will be either prefix + space + firstname + space + lastname or firstname + space + lastname, then couldn't you just use the Split function? As below (untested):

string[] parts = fullName.Split(' ');
if (parts.Length > 2)
{
    label1.Text = parts[0];
    label2.Text = parts[1];
    label3.Text = parts[2];
}
else
{
    label2.Text = parts[0];
    label3.Text = parts[1];
}

If you can't be sure about the name entries though, you might want something more robust, like three different Textboxes for each parameter.

Comments

0

The right solution would be to have three separate text boxes for prefix, first name & last name. For now, this should work.

This is not very "short-n-simple", but handles almost all cases.

string prefix = string.Empty;
string firstName = string.Empty;
string lastName = string.Empty; //no MiddleName, as per your question

var fullName = "Dr. Riwaz N David"; //example

if (string.IsNullOrWhiteSpace(fullName))
    return; //show error
var words = fullName.Trim().Split(' ');

if (words.Length == 1) //only firstname
{
    firstName = words[0];
    return;
}
if (words.Length == 2) //firstname, lastname
{
    firstName = words[0];
    lastName = words[1];
    return;
}

lastName = words[words.Length - 1]; //David
if (Regex.Match(words[0], "^[A-Za-z]{2,3}[.]$").Success) //Mrs. Dr. etc
{
    prefix = words[0]; //Dr.
    firstName = string.Join(" ", words.Skip(1).Take(words.Length - 2)); //Riwad N
}
else //when fullName = "Riwaz N David" no prefix
{
    firstName = string.Join(" ", words.Take(words.Length - 1)); //Riwaw N 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.