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.
C#

Avatar of undefined
Last Comment
kmaicorp
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 );
Avatar of bob20062006
bob20062006

       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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo