-1

So say I have a two word name in a DataRow with a space in between words. The entire name is represented as "poc" in the datarow. I can get the poc to display the full name no problem. The code looks like this :

foreach (DataRow mitRow in dtMIT.Rows)
    listboxMit.Items.Add(new ListItem(mitRow["poc"].ToString() 
            + " (" + mitRow["email"].ToString() + ")", 
        mitRow["userName"].ToString()));

Now I have code here that gets the "poc" and displays the first name and last name on the webpage:

foreach (DataRow mitRow in dtMIT.Rows)
    firstNameMIT.Text = mitRow["poc"].ToString();

My question is, HOW do I write the code so that it gets only the LAST NAME after the space. The whole name format is this: John Doe

because separating the first and last name to display them in two separate text boxes is proving difficult. I have tried adding another foreach statement such as this :

foreach (DataRow mitRow in dtMIT.Rows)
    lastNameMIT.Text = mitRow[" "].Tostring();

I have a char [] delimiterChars = {" "}; above everything too.

My goal is to take the values that is stored in "poc", the first and last name, then display them in separate text boxes on the webpage.

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
  • Q: What is a "poc"??? More importantly: Q: Do you have separate columns for "firstname" and "lastname" in your database, or are you trying to split a name by "space". If the latter, why not just use [string.Split()](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=net-5.0)? – paulsm4 Jul 09 '21 at 18:58
  • What if a person has two names? What if last name is done by two words? I really hope you have different fields in database for first and last name... – Marco Jul 09 '21 at 19:58

1 Answers1

0
foreach (DataRow mitRow in dtMIT.Rows)
{
    string[] fullName = mitRow["poc"].ToString().Split(' ');
    firstNameMIT.Text = fullName.Length < 1 ? "" : fullName[0];
    lastNameMIT.Text = fullName.Length < 2 ? "" : String.Join(" ", fullName.Skip[1]);
}
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
  • What if a person has two names? What if last name is done by two words? – Marco Jul 09 '21 at 19:57
  • I said in the description if the name structure [John Doe] . This is what is said in the question – Meysam Asadi Jul 09 '21 at 20:00
  • _The whole name format is this: John Doe_ IMHO means that first name appears before last name; but if database contains real names, soon or late, splitting by space and getting only two items will result in a mistake. Don't get it wrong, it's not your fault, problem is in the database design :( – Marco Jul 09 '21 at 20:04
  • This code crashes if mitRow["poc"] does not contain space. This code gives wrong result for « Jean Marc de la Roche Bobois ». –  Jul 09 '21 at 20:06
  • @GuyatMercator: Meysam code is a wrong approach to a very wrong design. Problem is, if database doesn't store first and last name in different columns, there's no way to split a full name in the right mode, unless there's only one first name and only one last name. In a real world this won't happen, not always, so there's no solution but assuming something (eg, just one first name and the other part is last name, or something similar) – Marco Jul 09 '21 at 20:11
  • Yes, there is a problem with database design. I entered this code in the mode of choosing between bad and worse. – Meysam Asadi Jul 09 '21 at 20:14
  • @Marco real world is sometimes not the ideal world imagined by SO gourou’s. In an accounting package or ERP, it’s common not to separate first name and last name. The above answer should at least test if mitRow["poc"].ToString().Split(' ').Length >= 2. –  Jul 09 '21 at 20:17
  • @GuyatMercator you're right, the more I look at Meysam edited code, the worse it goes. Meysam, your code right now has two huge mistakes: 1) `if(fullName[1] != null)` throws an exception if the array is just one item long; 2) you discard part of full name if you don't take in account fullName[2], fullName[3] and so on if they exist after `Split`... at least decide to get one item for first name and **everything else** for last name – Marco Jul 09 '21 at 20:36
  • 1
    I think a better solution for lastName could be `var lastName = fullName.Length < 2 ? "" : String.Join(" ", fullName.Skip[1]);`. I can't test it, but it should work. – Marco Jul 09 '21 at 20:45
  • @Marco Thank you very much for your guidance. I applied your solution in response. – Meysam Asadi Jul 09 '21 at 20:49
  • 1
    Meysam, you're welcome. SO is a community where people likes to help and teach others, it's far beyond _points_. So my comments were meant to help you understand what you did wrong, even because you would have probably done such mistakes even in your softwares :) – Marco Jul 09 '21 at 20:52