Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
"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.
 
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 23-Sep-11 8:45am
rajh7423
Edited 23-Sep-11 8:48am
v2
Comments
Simon Bang Terkildsen at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
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).
  Permalink  
v2

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 23 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100