Click here to Skip to main content
15,886,801 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
"AGRICULTURE(FOOD,GRAIN,WHEAT POOLS)AgPro Grain2"

I need to split that string in to 3 parts.

1) AGRICULTURE(FOOD,GRAIN,WHEAT POOLS)
2) AgPro Grain2
3) 2

I tried the following but nothing split the way I want.

C#
char seperator      = '_';

char[] seperator = new Char[] { '.' };
char[] seperator = new Char[] {'.'};

char[] seperator = new Char[] { ',', ' ' };
char[] seperator      = new char[]{};
char[] seperator      = new char[]{};
char seperator      = {};


What is the right syntax that can split it the way I want and return in the array?
Posted
Updated 23-Sep-11 7:48am
v2
Comments
Simon Bang Terkildsen 23-Sep-11 13:50pm    
There is no rule, in your post, as to what should indicate that the string should be split.
Tells us exactly how you want your string to be split, as the post is right now you can't do what you want.

I would use a regex: This may not work exactly, it depends on your criteria, which you don't specify:
^(?<description>[^)]*\))(?<shortname>[^\d]*)(?<count>\d+)$</count></shortname></description>
This separates it into three groups.
 
Share this answer
 
Your string doesn't have an identifiable pattern, so that means you're going to have to do it by hand. String.Split can only help a little.
C#
string  str = "AGRICULTURE(FOOD,GRAIN,WHEAT POOLS)AgPro Grain2";
string[] temp =  str.Split(')');
temp[0] += ")";
string temp2 = str.Substring(str.Length - 1);
string[3] result = new string("","","");
Array.Copy(temp, 0, result, 0, temp.Length);
result[2] = temp2;

That should give you what you want (may need to be tweaked because I didn't test it).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900