-1
PO1*1*120*EA*9.25*TE*CB*065322-117*PR*RO*VN*AB3542
PID*F****SMALL WIDGET
PO4*4*4*EA*PLT94**3*LR*15*CT
PO1*2*220*EA*13.79*TE*CB*066850-116*PR*RO*VN*RD5322
PID*F****MEDIUM WIDGET
PO4*2*2*EA
PO1*3*126*EA*10.99*TE*CB*060733-110*PR*RO*VN*XY5266
PID*F****LARGE WIDGET
PO4*6*1*EA*PLT94**3*LR*12*CT
PO1*4*76*EA*4.35*TE*CB*065308-116*PR*RO*VN*VX2332
PID*F****NANO WIDGET

this is the EDI 850 Purchase order sample and in every line starting with "PO1" there is the Quantity of product in 2nd place(starting from index 0) and unit price is in 4th place(starting from index 0). My problem is I want every data to show in my console app. So how will I do that? I'm a beginner in C# and I feel frustrated my mind is not working. Please help me.

DerStarkeBaer
  • 669
  • 8
  • 28
  • 4
    It is unclear what's your actual problem and how much you already know. Do you have any significant programming experience at all? Do you struggle to present a list of items in a console app? Do you struggle to parse EDI data? You should state what you already achieved and what specific problem you have. – Hans-Martin Mosner Jun 22 '20 at 05:39
  • What is first line of text (P01, PID, P04)? It looks like the data consists of three lines but need to know which is first line to properly group the data. – jdweng Jun 22 '20 at 08:23
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. Also check the [help/on-topic] to see what questions you can ask. Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236). – Progman Jun 22 '20 at 11:22

1 Answers1

1

Regex would be one option but I think plain C# is simpler.

foreach (string line in lines)
{
    // skip lines that do not start with the correct prefix
    if (!line.StartsWith("PO1"))
        continue;

    string[] parts = line.Split("*");
    int quantity = int.Parse(parts[2]);
    decimal price = decimal.Parse(parts[4]);
    Console.WriteLine($"{quantity} x ${price}");
}

Notice how I used decimal for the price instead of double. You can read about this here: decimal vs double! - Which one should I use and when?

Since we use the Parse() method of int and decimal an exception will be thrown then the value cannot be parsed. If you don't want that use TryParse() like this:

bool success = int.TryParse(parts[2], out int quantity);
koryphaee
  • 11
  • 2
  • _[Stance on answering “bad” questions](https://meta.stackoverflow.com/questions/281793/stance-on-answering-bad-questions)_ and _[Should one advise on off-topic questions?](https://meta.stackoverflow.com/questions/276572/should-one-advise-on-off-topic-questions)_ –  Jun 22 '20 at 05:51