C#-APP: Splitting a string by a character

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Iridium
    New Member
    • Nov 2007
    • 6

    C#-APP: Splitting a string by a character

    Hello, I have a quick question:

    I have a string in the following format:

    |001|ACOMMAND|a rgument1|argume nt2|

    Now I need to get 'argument1' and 'argument2'. For the other strings I used StringBuilder.R emove, since they both have a fixed lengths. But 'argument1' and 'argument2' dont have a fixed length, so I cant use that.

    Is there a function that lets me split the string by the '|'?

    Thanks in advance!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    There is indeed a string split() function.

    Consider:
    Code:
    string mys="001|something| something else| super duper!| last one";
    string[] allpieces=mysSplit('|');//note that I used single quotes there to designate a character
    //now allpieces will be like this:
    //allpieces[0]= 001
    //allpieces[1]= something
    //allpieces[2]=  something else
    //allpieces[3]=  super duper!
    //allpieces[4]=  last one
    Also, there is a .IndexOf() function that will tell you the position of a character in a string ( -1 if it doesn't exist in the string) and then you could use .Substring() to pick which pieces you want.

    Comment

    Working...