格式化数组时在 "string.join()" 中添加新行?
Adding a new line in "string.join()" when formatting arrays?
你好,我是一名新手程序员,我正在尝试以在一组特定数组末尾有换行符的方式格式化数组。我目前有 4 个单独的数组,我想将数组中的每个项目排列成特定的模式。我已经完成了这项任务,但现在我很困惑,因为它们都在一条线上。让我举个例子:(顺便说一句,我是在一个datagridview上做的)
(This is what I want to happen)
Binder Clips Small pc 12 1260
Selleys All Clear pc 12 2400
(This is what I am getting)
Binder Clips Small pc 12 1260 Selleys All Clear pc 12 2400
这是我的代码:
//I get these from a datagridview
var items = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[1].Value.ToString().Trim())
.ToArray();
var units = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[2].Value.ToString().Trim())
.ToArray();
var quantity = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[6].Value.ToString().Trim())
.ToArray();
var prices = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[8].Value.ToString().Trim())
.ToArray();
//this is what I use to sort out the pattern that I want the arrays to be in
string[] concat = new string[items.Length * 4];
int index = 0;
for (int i = 0; i < items.Length; i++)
{
concat[index++] = items[i];
concat[index++] = units[i];
concat[index++] = quantity[i];
concat[index++] = prices[i];
}
// and this is where I am stuck because I can just put \n, it would ruin the format even more
cartitems.Text = string.Join(" ", concat);
我也试过这样做:
int j = 0;
string str = "";
foreach (var item in concat)
{
str += item;
if (j <= concat.Length - 1)
{
if (j % 3 == 0)
str += " ";
else
str += "\n";
}
j++;
}
它有点完成工作,但换行符到处都是。
这就是我的项目的样子,因此您可以更好地了解我从 4 个数组中获取数据的位置:
基本上是产品名称、单位、数量和行总计
最后我存储在一个标签上,这样我就可以看到它的格式是怎样的:
总结了我的问题,真心希望你能帮帮我这样的新手,我感觉答案很简单,我只是没经验。
作为一般规则,您应该将 数据结构 (数据的存储方式,此处作为值数组实现)与数据 分开表示(在本例中,写入列表框)。
C# 是一种面向对象的语言,所以我们不妨利用它,对吗?
为您的项目创建 class。
class Item
{
public string Name { get; set; }
public string Unit { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
override public string ToString() {
return $"{Name} {Unit} {Quantity} {Price}";
}
}
这就是您加载数组的方式。
Item[] concat = new Item[items.Length];
int index = 0;
for (int i = 0; i < items.Length; i++) {
concat[index++] = new Item {
Name = items[i],
Unit = units[i],
Quantity = quantity[i],
Price = prices[i]
};
}
这是将项目列表添加到列表框的方法。
foreach(Item item in concat) {
listBox.Items.Add(item);
}
你好,我是一名新手程序员,我正在尝试以在一组特定数组末尾有换行符的方式格式化数组。我目前有 4 个单独的数组,我想将数组中的每个项目排列成特定的模式。我已经完成了这项任务,但现在我很困惑,因为它们都在一条线上。让我举个例子:(顺便说一句,我是在一个datagridview上做的)
(This is what I want to happen)
Binder Clips Small pc 12 1260
Selleys All Clear pc 12 2400
(This is what I am getting)
Binder Clips Small pc 12 1260 Selleys All Clear pc 12 2400
这是我的代码:
//I get these from a datagridview
var items = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[1].Value.ToString().Trim())
.ToArray();
var units = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[2].Value.ToString().Trim())
.ToArray();
var quantity = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[6].Value.ToString().Trim())
.ToArray();
var prices = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[8].Value.ToString().Trim())
.ToArray();
//this is what I use to sort out the pattern that I want the arrays to be in
string[] concat = new string[items.Length * 4];
int index = 0;
for (int i = 0; i < items.Length; i++)
{
concat[index++] = items[i];
concat[index++] = units[i];
concat[index++] = quantity[i];
concat[index++] = prices[i];
}
// and this is where I am stuck because I can just put \n, it would ruin the format even more
cartitems.Text = string.Join(" ", concat);
我也试过这样做:
int j = 0;
string str = "";
foreach (var item in concat)
{
str += item;
if (j <= concat.Length - 1)
{
if (j % 3 == 0)
str += " ";
else
str += "\n";
}
j++;
}
它有点完成工作,但换行符到处都是。
这就是我的项目的样子,因此您可以更好地了解我从 4 个数组中获取数据的位置:
基本上是产品名称、单位、数量和行总计
最后我存储在一个标签上,这样我就可以看到它的格式是怎样的:
总结了我的问题,真心希望你能帮帮我这样的新手,我感觉答案很简单,我只是没经验。
作为一般规则,您应该将 数据结构 (数据的存储方式,此处作为值数组实现)与数据 分开表示(在本例中,写入列表框)。
C# 是一种面向对象的语言,所以我们不妨利用它,对吗?
为您的项目创建 class。
class Item
{
public string Name { get; set; }
public string Unit { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
override public string ToString() {
return $"{Name} {Unit} {Quantity} {Price}";
}
}
这就是您加载数组的方式。
Item[] concat = new Item[items.Length];
int index = 0;
for (int i = 0; i < items.Length; i++) {
concat[index++] = new Item {
Name = items[i],
Unit = units[i],
Quantity = quantity[i],
Price = prices[i]
};
}
这是将项目列表添加到列表框的方法。
foreach(Item item in concat) {
listBox.Items.Add(item);
}