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!
-
Add an example of what you want from a sample name string.Arghya C– Arghya C2015-09-15 04:16:07 +00:00Commented 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.user1162766– user11627662015-09-15 04:16:26 +00:00Commented 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 = SocksSallyD– SallyD2015-09-15 04:18:58 +00:00Commented Sep 15, 2015 at 4:18
-
2My 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"user1162766– user11627662015-09-15 04:30:34 +00:00Commented Sep 15, 2015 at 4:30
4 Answers
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.
1 Comment
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
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
}