Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this code here and I am trying to read the indexes of the array I need it to read all not just one at a time

Is this possible?

C#
string s = textBox1.Text;

           string[] arr = new string[]
   {
       "cat",
       "dog",
       "panther",
       "tiger"
   };

          if (s.Contains(arr[0]))
           {
string v = s.Replace(arr[0], "*********");
              //show message with work that isnt allowed
              MessageBox.Show("Can not use the word " + arr[0]);
               textBox2.Text = v;
           }
           else
           {
               textBox2.Text = s;
           }
Posted
Comments
PIEBALDconsult 20-Sep-15 0:21am    
Ooorrr... convert your array of strings into a Regular Expression.

I'd do something like this.


C#
string st = "Bob had a cat and a tiger";
           string[] arr = {
               "cat",
               "dog",
               "panther",
               "tiger"
           };
           foreach (var name in arr)
           {
               if (st.Contains(name))
               {
                   st = st.Replace(name, "****");
                   Console.WriteLine("Cannot use the word " + name);
               }

           }

           Console.WriteLine(st);//Bob had a **** and a ****

           //using linq
           st = "Bob had a dog and a panther";

               foreach (var name in arr.Where(name => st.Contains(name)))
               {
                   st = st.Replace(name, "****");
                   Console.WriteLine("Cannot use the word " + name);
               }
               Console.WriteLine(st);

            //using a StringBuilder with no 'where' clause
           st = "Bob had a dog and a panther";
           StringBuilder msb=new StringBuilder(st);

               foreach (var name in arr)
               {
                   msb.Replace(name, "****");
                   Console.WriteLine("Cannot use the word " + name);
               }
               Console.WriteLine(msb);
 
Share this answer
 
v2
use a for loop to read

C#
for(int i = 0; i < arr.Length; i++){
 // arr[ i ]  do something here
}
 
Share this answer
 
Arrays and many other collections implement an iterator interface that can help loop through the collection (or array).
Here are some examples of iterating through these arrays -
Iterators (C# and Visual Basic)[^]
How to: Iterate through an array[^]
 
Share this answer
 
ok thanks for the help guys I was able to use solution 3 and this is what I came up with

string s = textBox1.Text;

           string[] arr = new string[]
  {
      "cat",
      "dog",
      "panther",
      "tiger"
  };
          foreach (var name in arr)
          {
              if (s.Contains(name))
              {
                  s = s.Replace(name, "****");
               //   MessageBox.Show("Cannot use the word " + name);

           }
           else
           {
               textBox2.Text = s;
           }



however it will work but it don't read the last word in this case tiger
 
Share this answer
 
v2

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