Click here to Skip to main content
16,007,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello all,

Hope that you are having a great times.

my question is :

i have the following string for example "

(0)1002(10)1

this string should be divided into 2 strings as follows :

string item_code = 1002 ; always what comes after(0) is the item code
string unit = 1 ; always what comes after(10) is the unit


Please help to do this one

Best Regards,
Hadi
Posted
Comments
hadi_753 4-Jun-13 9:30am    
Splitting not spitting
joshrduncan2012 4-Jun-13 9:32am    
Help you with what? What have you tried to do to accomplish this task? You haven't shown us any attempt on your part first.
hadi_753 4-Jun-13 10:28am    
i searched .. bu it's little complicated .. maybe we can solve this problem using regex ...

anyway .. thanks for passing by :)
Richard MacCutchan 4-Jun-13 9:50am    
You could use the split method, or a regular expression (aka Regex).
hadi_753 4-Jun-13 10:29am    
can you provide me with example...

thanks :)

Try something like this:
String data = "(0)1002(10)1";
String[] values = data.Split("(10)");

Check the length of the array to make sure the values are present.
index 0: remove '(0)' from the beginning
index 1: unit

Good luck!
 
Share this answer
 
Comments
hadi_753 4-Jun-13 10:14am    
we cannot split on (10) because we may add other prefix later on :)
any way ... thanks for trying to help me ....
best regards,
E.F. Nijboer 4-Jun-13 10:17am    
Then simply split on ")" instead and go with the 4 resulting values instead. index 0 can be ignored and index 2 will be your prefix after removing the "(".
hadi_753 4-Jun-13 10:30am    
can you provide me with sample code ..

sorry to be annoying .

best regards,
hadi_753 4-Jun-13 10:32am    
even prefix order is not fixed...
E.F. Nijboer 4-Jun-13 11:24am    
Just try to split using ")" and put a breakpoint after that to check out the contents of the array. You can figure it out easily from there.
C#
string str = string.Empty;
       str = "(0)1002(10)1";
       string str1 = string.Empty;
       string str2 = string.Empty;
       int l = str.Length;
       int f1 = 0;
       int f2 = 0;
       for (int i = 0; i < l;i++ )
       {

           if (str[i].ToString() == "(")
           {
               f1 = f1 + 1;
           }
           else if (str[i].ToString() == ")")
           {
               f2 = f2 + 1;
           }
           if(i>2 && f1==1)
           {
               str1 = str1 + str[i].ToString();
           }
           if(str[i].ToString() != ")" && f2==2)
           {
               str2 = str2 + str[i].ToString();
           }

       }
 
Share this answer
 
Comments
hadi_753 4-Jun-13 10:27am    
I think we are getting closer to the solution ..

the problem here is that we may add other prefix such as (55) for the price .. for examples

here we will get problem ..

many thanks for trying to help me ..

waiting your reply..
best regards,
hadi
string str = "(0)1002(10)1;

string item_code = str.Substring(str.IndexOf("(0)"), str.IndexOf("(10)")).Replace("(0)", "");
string unit = str.Substring(str.IndexOf("(10)")).Replace("(10)", "");
 
Share this answer
 
Comments
hadi_753 4-Jun-13 10:15am    
the problem is sometimes ... unit comes first.. it's not fixed !!
got any idea ??

thanks for trying to help me :)

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