Давайте метод извлечения :
private static string FormatMe(string value) {
if (string.IsNullOrEmpty(value))
return value;
else if (!value.StartsWith("PN="))
return value;
int lot = value.IndexOf(";LOT=", 3);
if (lot >= 0)
return $"PN_EQ: {value.Substring(3, lot - 3)} LOT_EQ: {value.Substring(lot + 5)}";
else
return $"PN_EQ: {value.Substring(3)}";
}
Давайте посмотрим:
string[] item_list = {
"PN=12345678",
"PN=1234-5678-0001",
"PN=1234-4321-0001;LOT=xyz"
};
string report = string.Join(Environment.NewLine, item_list
.Select(item => FormatMe(item)));
Console.Write(report);
Результат:
PN_EQ: 12345678
PN_EQ: 1234-5678-0001
PN_EQ: 1234-4321-0001 LOT_EQ: xyz
Время создания Label
с (при условии WinForms
):
using System.Linq;
...
string[] item_list = {
"PN=12345678",
"PN=1234-5678-0001",
"PN=1234-4321-0001;LOT=xyz"
};
// let's query item_list:
// for each item within item_list
// we create a Label
// all the labels we materialize in a List
List<Label> labels = item_list
.Select((item, index) => new Label() {
Text = FormatMe(item),
Location = new Point(10, 50 + 40 * index), //TODO: put the right locations here
AutoSize = true,
Parent = this, // Instead of this.Controls.Add(...);
})
.ToList();
Редактировать: Альтернативный номер Linq , но l oop решение:
List<Label> labels = new List<Label>(item_list.Length);
for (int index = 0; index < item_list.Length; ++index)
labels.Add(new Label() {
Text = FormatMe(item_list[index]),
Location = new Point(10, 50 + 40 * index), //TODO: put the right locations here
AutoSize = true,
Parent = this, // Instead of this.Controls.Add(...);
});