在ForEach循环中连接字符串 [英] Concatenate Strings In ForEach Loop

查看:142
本文介绍了在ForEach循环中连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为每个循环迭代两次,并将值连接成一个字符串。  我有以下语法,但我的问题是数据被追加两次。

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

推荐答案

这里

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();
"{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);


这篇关于在ForEach循环中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆