简体   繁体   中英

How do i split a name string into two textboxes

This is in c#

I have 2 textboxes for firstname and lastname. I wish to split a name string which into the appropriate textbox which it belongs to, but the problem is there are many combinations of what the name string can be. It can either be, "Mr John Smith", or "Mr Smith", for these the result i get is ok, the problem im having now is when splitting a string such as "Mrs and Mrs Smith" The split does not work as planned as i get "Mr" in the first textbox and i get "and Mrs Smith" in the second textbox.

Here is my function below, can anyone help as I will like to have the firstname textbox with the firsy name if there is one and the lastname textbox with value of the lastname if the string is something like "Mr and Mrs Smith" or "Mr & Mrs John Smith"

string name = "Mr and Mrs Smith";
string[] titles = new string[] {"Mr", "Miss", "Mrs", "Ms", "Master", "Mr and Mrs", 
           "Mr & Mrs", "Lady", "Lord", "Prof", "Proffessor", "Ma'am", "Madame"};
foreach (string title in titles)
{
  if (name.StartsWith(title) && !name.Equals(title, StringComparison.CurrentCultureIgnoreCase))
  {
    var titleLength = title.Length;
    var titlelessName = name.Substring(titleLength + 1);
    var spaceIndex = titlelessName.IndexOf(' ');
    if (spaceIndex > -1 && spaceIndex >= 0)
    {
      var firstName = titlelessName.Substring(0, spaceIndex);
      var lastName = titlelessName.Substring(spaceIndex + 1);
      txtFname.Text = firstName;
      txtLname.Text = lastName;
    }
  }
  else if (!name.StartsWith(title) && name.Contains(' '))
  {
    var firstName = name.Substring(0, name.IndexOf(' '));
    var lastName = name.Substring(name.IndexOf(' '));
    txtFname.Text = firstName;
    txtLname.Text = lastName;
  }
}

You are trying to handle all instances of your input as something which comes in the form of {firstname} {lastname} . Now the reality is that you accept a whole lot of input that comes in differing shapes and sizes. So you could either bash your head bloody trying to force your input to behave like you want it, or you can adapt and change the solution to fit the input.

You will never be able to figure out all possible combinations of first name, last name and title. What about people who enter Duke Leopold von Haussknauf, Phd ?

I know this isn't the answer you're looking for, but you should consider changing the solution in some way. Maybe present it in a single box instead, or make people input title, first and last name separately to begin with?

Your problem is that the logic of you loop isn't correct.

You first check if the title matches, if it doesn't you split on the space in the name. The problem here is that you shouldn't do that until you've checked every title.

What's worse, is that if the title in you name isn't the last title in your array, then the next cycle through the array will end up doing the second condition in your loop and overwrite the correct behavior. You need to exit the loop once you've found a matching title!

So, something like this (untested code):

    string name = "Mr and Mrs Smith";
    string[] titles = new string[] {"Mr", "Miss", "Mrs", "Ms", "Master", "Mr and Mrs", "Mr & Mrs", "Lady", "Lord", "Prof", "Proffessor", "Ma'am", "Madame"};
    var sortedTitles = from s in titles
         orderby s.Length descending
         select s;
    bool foundTitle = false;
    foreach (string title in sortedTitles)
    {
        if (name.StartsWith(title) && !name.Equals(title, StringComparison.CurrentCultureIgnoreCase))
        {
            var titleLength = title.Length;
            var titlelessName = name.Substring(titleLength + 1);
            var spaceIndex = titlelessName.IndexOf(' ');
            if (spaceIndex > -1 && spaceIndex >= 0)
            {
                var firstName = titlelessName.Substring(0, spaceIndex);
                var lastName = titlelessName.Substring(spaceIndex + 1);
                txtFname.Text = firstName;
                txtLname.Text = lastName;
            }
            foundTitle = true;
            break;
        }
    }
    if (!foundTitle && name.Contains(' '))
    {
        var firstName = name.Substring(0, name.IndexOf(' '));
        var lastName = name.Substring(name.IndexOf(' '));
        txtFname.Text = firstName;
        txtLname.Text = lastName;
    }

And, of course, this isn't taking into account that your list of titles is far from exhaustive and doesn't include a lot of varieties that you might encounter. What about Mr. and Mrs. Smith ?

我对您的前任假设不满意:史密斯先生将先生显示为姓氏,我建议使用其他方式输入您的数据,例如下拉列表中的先生,女士...等,请仔细考虑您的要求

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM