Reputation: 9
I have these records:
Value Part Qty
A PartA QtyA
B PartB QtyB
C PartC QtyC
I'm concatenating each column in different strings:
stringValue = A~B~C
stringPart = PartA~PartB~PartC
stringQty = QtyA~QtyB~QtyC
I want to split each string to get the original row value according to the position but I'm not sure how to do it any insight would be appreciate it.
Thanks
Upvotes: 0
Views: 574
Reputation: 19384
Create some data storage
class Model
{
string Value {get; set;}
string Part {get; set;}
string Quantity {get; set;}
}
Parse and format
private List<Model> _list = new List<Model>();
void MakeList()
{
string[] values = "A~B~C".Split("~".ToCharArray(), StringSplitOptions.None);
string[] parts = "PartA~PartB~PartC"("~".ToCharArray(), StringSplitOptions.None);
string[] qtys = "QtyA~QtyB~QtyC"("~".ToCharArray(), StringSplitOptions.None);
for (int i = 0; i < values.Length; i++)
{
var item = new Model(){ Value = values[i], Part = parts[i], Quantity = qtys[i]};
_list.Add(item);
}
}
string[] GenerateCommaSeparatedRecords()
{
return _list.Select(x => string.Format("{0},{1},{2}", x.Value, x.Part, x.Quantity)).ToArray();
}
You can generate any output you wish
Upvotes: 0
Reputation: 61
String[] yourfirstrow = stringValue.Split(new string[] { "~" }, StringSplitOptions.None);
//yourfirstrow[0] first cell value
//yourfirstrow[1] second cell value
//yourfirstrow[2] third cell value
Upvotes: 1