Link to home
Start Free TrialLog in
Avatar of sbornstein2
sbornstein2

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Expert1701
Expert1701

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vlit
vlit

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 '|'.

Avatar of sbornstein2

ASKER

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.
I need to pass all 5
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
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
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
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]
  }