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.
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 );