String Split Pipe Delimiter

Hi all.  Here is what I am trying to do.  I have a pipe delimited string that will have Order IDs separated by a pipe.  The maximum order ids will be up to 5 of them.   I need to somehow set 5 int variables while looping through the Order IDs.  Here is what I have so far:

string stringListItems = pipedOrderIdent;
string[] pipedListItems = stringListItems.Split(Convert.ToChar("|")); //get pipe separated Order Idents
                  
                        for (int i=0;i<pipedListItems.Length;i++)//
                        {
                              pipedListItems[i];
                        }

what I am trying to do is get 5 int variables where if there is not a pipe field for it the value will stay 0.

int OrderID1 = 0;
int OrderID2 = 0;
int OrderID3 = 0;
int OrderID4 = 0;
int OrderID5 = 0;

So if there was two order ids within the pipe I would want to set OrderID1 and OrderID2 like

pipedOrderIdent = 100002|100056

I would want:
int OrderID1 = 100002;
int OrderID2 = 100056;
int OrderID3 = 0;
int OrderID4 = 0;
int OrderID5 = 0;

thanks all for any help
sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Expert1701Commented:
If you want to keep your OrderID1, OrderID2... variables:

  string[] pipedListItems = stringListItems.Split(Convert.ToChar("|"));  

  int OrderID1 = pipedListItems.Length > 0 ? Convert.ToInt32(pipedListItems[0]) : 0;
  int OrderID2 = pipedListItems.Length > 1 ? Convert.ToInt32(pipedListItems[1]) : 0;
  int OrderID3 = pipedListItems.Length > 2 ? Convert.ToInt32(pipedListItems[2]) : 0;
  int OrderID4 = pipedListItems.Length > 3 ? Convert.ToInt32(pipedListItems[3]) : 0;
  int OrderID5 = pipedListItems.Length > 4 ? Convert.ToInt32(pipedListItems[4]) : 0;

Or another solution using an array:

  string[] pipedListItems = stringListItems.Split(Convert.ToChar("|"));

  int[] OrderIDs = new int[5];

  for (int i = 0; i < pipedListItems.Length; i++)
    OrderIDs[i] = Convert.ToInt32(pipedListItems[i]);

  //int OrderID1 = OrderIDs[0];
  //int OrderID2 = OrderIDs[1];
  //int OrderID3 = OrderIDs[2];
  //int OrderID4 = OrderIDs[3];
  //int OrderID5 = OrderIDs[4];
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vlitCommented:
Just a small addition. I see you're using Convert.ToChar("|") in your Split method. You probably don't know this, but:

"|" (double quotes) = the string |
'|' (single quotes) = the char |

So, you don't need the whole Convert.ToChar() stuff, just replace Convert.ToChar("|") with '|'.

0
sbornstein2Author Commented:
Hi Expert thanks for the response.  Also thanks Vlit as well.  What is the best way you think should I use the array?  Also if I do that when I go to pass those variables to another void how do I do that like OrderID1 etc.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

sbornstein2Author Commented:
I need to pass all 5
0
sbornstein2Author Commented:
I actually need the loop because I need to grab the value for the pipe split as well and pass that to another proc within that loop so maybe the array is best
0
sbornstein2Author Commented:
so I need the for loop like:

 for (int i=0;i<pipedListItems.Length;i++)//
                    {
                         pipedListItems[i];
                    }

and then splitting it out within the above loop where it shows pipedListItems I need to pass the value to a proc as well as setting it to the orderID
0
sbornstein2Author Commented:
expert that is what I was looking for the loop you had.  If you can just verify how I pass the variables as the 5 order ids when I need to access the variables.  Thanks
0
Expert1701Commented:
sbornstein2, you can pass the variable in the same way it was declared:

  void TestMethod(int[] OrderIDs)
  {
    //Then you can use OrderIDs[0] .. OrderIDs[4]
  }
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.