Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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.
0
jonnyboy69
Asked:
jonnyboy69
1 Solution
 
htang_usCommented:
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 );
0
 
bob20062006Commented:
       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";
           
            }
        }
0
 
kmaicorpCommented:
Here's a method using regex like you asked. This work for any length string.

public static void Main()
{
      System.String strToTest = "111001001";
      System.Text.RegularExpressions.Regex objOne = new Regex("1");
      System.Text.RegularExpressions.Match match = null;
      int idx = 0;
      do
      {
            match = objOne.Match(strToTest, idx);
            if (match.Success)
            {
                  Console.WriteLine("Match at pos: " + match.Index);
                  idx = match.Index + 1;
            }
      } while (match.Success);
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now