none
Concatenate Strings In ForEach Loop RRS feed

  • Question

  • I want to iterate two for each loops and concatenate the values into one string.  I have the below syntax, but my issue is that the data is appended twice.

    How should I write this differently so that the data is only written one time?

    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("ItemsSold", typeof(string));
    table.Columns.Add("ItemIDs, typeof(string));
    table.Rows.Add("James Jo", "Shirt; Hat; Socks;", "SH11; HA22; SO33");
    
    var itemsSold = table.Rows[0].Field<string>("ItemsSold").Split(new string[] {"; "}, StringSplitOptions.RemoveEmptyEntries);
    var ItemIDs = table.Rows[0].Field<string>("ItemIDs").Split(new string[] {"; "}, StringSplitOptions.RemoveEmptyEntries);
    
    string displayformat = "";
    
    foreach (string is in itemsSold)
    {
        foreach (string id in ItemIDs)
        {
            string s = is.Replace(";", "");
            string d = id.Replace(";", "");
            displayformat += s + " - " + d + Environment.NewLine;
        }
    }
    
    Console.WriteLine(displayformat);
    
    Will write - 
    Shirt - SH11
    Hat - HA22
    Socks - SO33
    Shirt - SH11
    Hat - HA22
    Shirt - SH11

    Wednesday, January 2, 2019 12:05 AM

All replies

  • Here is something that will work with one for

    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("ItemsSold", typeof(string));
    table.Columns.Add("ItemIDs", typeof(string));
    table.Rows.Add("James Jo", "Shirt; Hat; Socks;", "SH11; HA22; SO33");
    
    var itemsSold = table.Rows[0].Field<string>("ItemsSold").Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
    var ItemIDs = table.Rows[0].Field<string>("ItemIDs").Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
    
    
    var sb = new StringBuilder();
    for (int index = 0; index < itemsSold.Length; index++)
    {
        sb.AppendLine($"{itemsSold[index]} - {ItemIDs[index]}");   
    }
    
    var displayFormat = sb.ToString();

    Edit If not using a current version of the Framework then try this too.

    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("ItemsSold", typeof(string));
    table.Columns.Add("ItemIDs", typeof(string));
    table.Rows.Add("James Jo", "Shirt; Hat; Socks;", "SH11; HA22; SO33");
    
    var itemsSold = table.Rows[0].Field<string>("ItemsSold").Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
    var ItemIDs = table.Rows[0].Field<string>("ItemIDs").Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
    
    
    var sb = new StringBuilder();
    for (int index = 0; index < itemsSold.Length; index++)
    {
        sb.AppendLine(itemsSold[index] + " - " + ItemIDs[index]);   
    
    }
    
    var displayFormat = sb.ToString();
    Console.WriteLine(displayFormat);
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, January 2, 2019 12:32 AM
    Moderator
  • I want to iterate two for each loops and concatenate the values into one string.  I have the below syntax, but my issue is that the data is appended twice.

    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("ItemsSold", typeof(string));
    table.Columns.Add("ItemIDs, typeof(string));
    table.Rows.Add("James Jo", "Shirt; Hat; Socks;", "SH11; HA22; SO33");
    
    var itemsSold = table.Rows[0].Field<string>("ItemsSold").Split(new string[] {"; "}, StringSplitOptions.RemoveEmptyEntries);
    var ItemIDs = table.Rows[0].Field<string>("ItemIDs").Split(new string[] {"; "}, StringSplitOptions.RemoveEmptyEntries);
    
    string displayformat = "";
    
    foreach (string is in itemsSold)
    {
        foreach (string id in ItemIDs)
        {
            string s = is.Replace(";", "");
            string d = id.Replace(";", "");
            displayformat += s + " - " + d + Environment.NewLine;
        }
    }
    
    Console.WriteLine(displayformat);
    
    Will write - 
    Shirt - SH11
    Hat - HA22
    Socks - SO33
    Shirt - SH11
    Hat - HA22
    Shirt - SH11

    Is that the actual code you are working with? I ask because the third Add
    statement is missing a closing double quote:

    table.Columns.Add("ItemIDs, typeof(string));

    Should be:

    table.Columns.Add("ItemIDs", typeof(string));

    Also my version of VS2017 doesn't like this statement:

    foreach (string is in itemsSold)

    It puts red squiggly lines under "string" and "in" and gives compile errors,
    because it doesn't like your use of "is" in that statement. Changing it to:

    foreach (string its in itemsSold)

    eliminates those errors. Of course that also means that this statement:

    string s = is.Replace(";", "");

    must have a corresponding change:

    string s = its.Replace(";", "");

    After making the above changes and building, the code outputs:

    Shirt - SH11
    Shirt - HA22
    Shirt - SO33
    Hat - SH11
    Hat - HA22
    Hat - SO33
    Socks - SH11
    Socks - HA22
    Socks - SO33

    Which is what I would expect. 

    What were you expecting? I don't know how you got the output you posted:

    Will write - 
    Shirt - SH11
    Hat - HA22
    Socks - SO33
    Shirt - SH11
    Hat - HA22
    Shirt - SH11

    Note also that you should always state which version of VS and C# you are 
    using, as features, bugs, etc. vary between versions.

    - Wayne

    Wednesday, January 2, 2019 4:59 AM
  • Try this too:

    string displayformat = string.Join( Environment.NewLine, itemsSold.Zip( ItemIDs, ( s, d ) => s + " - " + d ) );

    Wednesday, January 2, 2019 6:01 AM