Link to home
Start Free TrialLog in
Avatar of jonnyboy69
jonnyboy69

asked on

Parsing string

I have a string with 9 charactors, each of which can either be 0 or 1. 1 determines a particular condition (related to the index of the char) is true, 0 = false.
Testing for this condition in SQL is fairly easy (in Access) as one can write something like "FieldToTest Like '1________' OR FieldToTest Like '_1_______'" etc....

My question is how would I test for this in code, ideally using regular expressions?

To explain... basically I want to test each of the charactors in the string to see if its 1 or 0. If its 1 I want to add to a global string which keeps track of all true conditions. So for example say one iterated all the 9 chars and positions 3 & 5 were 1 (all the others 0), the string result of the function would say "Conditions 3 and 7 were true"

I just cant seem to think this one through, any help would be greatly appreciated.
Avatar of htang_us
htang_us

how about a simple loop?

                  string myString = "001001001";
                  ArrayList al = new ArrayList();
                  for ( int i = 0; i < myString.Length; ++i )
                  {
                        char c = myString[i];
                        if ( c == '1' )
                        {
                              al.Add ( i );
                        }
                  }

                  string strOutput = "Condition ";
                  for ( int j = 0; j < al.Count; ++j )
                  {
                        if ( j > 0 )
                        {
                              strOutput += ", ";
                        }

                        strOutput += al[j];
                  }
                  strOutput += " are true.";
                  Console.WriteLine ( strOutput );
       private void ZeroOne() {
            string aa = "101101001";
            string newaa = "";
            if (newaa.IndexOf("1") != -1)
            {
                newaa = "Conditions ";
                for (int i = 0; i < aa.Length - 1; i++)
                {
                    if (aa[i].Equals("1"))
                    {
                        newaa += i + " and ";
                    }
                }
                newaa = newaa.Substring(0, newaa.Length - newaa.LastIndexOf("and "));
                newaa += " were true";
            }
            else
            {
                newaa = "No 1";
           
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of kmaicorp
kmaicorp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial