?
Solved

String Split Pipe Delimiter

Posted on 2006-04-12
8
Medium Priority
?
1,650 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:sbornstein2
  • 5
  • 2
8 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 2000 total points
ID: 16442743
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
 
LVL 1

Expert Comment

by:vlit
ID: 16444385
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
 

Author Comment

by:sbornstein2
ID: 16445574
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sbornstein2
ID: 16445580
I need to pass all 5
0
 

Author Comment

by:sbornstein2
ID: 16445595
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
 

Author Comment

by:sbornstein2
ID: 16445611
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
 

Author Comment

by:sbornstein2
ID: 16445685
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
 
LVL 11

Expert Comment

by:Expert1701
ID: 16447173
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question