Hello everyone ,

i have a text file that contains data like this :

a1 ;AT1 ;AT2 ,CE1 ,CT3
a2 ;AT1 ;AT1 ;CE1 ;CT3
a3 ,CO1 ;CO1 ;CT2 ,CT3

the file is big but that's an example

what i want to do is run through each line of the file and test these things :
1-if there is 2 or more duplicate with the same special character before the string (replace all the occurence with this character)
2-if there is 2 duplicate with a different special character before each one e.g in a3 ,CO1 and ;CO1 (replace them with t)

i wrote a code to do this , but it's not working as wanted ,

thanks for help

string[] tfile = File.ReadAllLines("test.txt");
          
           
            for (int rowIndex = 0; rowIndex < tfile.Length; rowIndex++)
            {
                
                arr = tfile[rowIndex].Split(' ');
                //Console.WriteLine(rowIndex + "\t" + arr[0]);
                int test = 0;
               
                string[] reslt = null;
                for (int ii = 0; ii < arr.Length; ii++)
                {
                    for (int jj = 0; jj < arr.Length; jj++)
                    {
                        if ((arr[ii] == arr[jj]) && (ii != jj))
                        {
                 //Console.WriteLine(arr[ii] + "\t" + arr[jj] + "\t" + ii + "\t" + jj);
                            string toreplace = arr[ii];
                            //Console.WriteLine(toreplace + test + "\n");
                            if (arr[jj].Contains("-"))
                                arr[Array.IndexOf(arr, toreplace)] = "-";
                              else arr[Array.IndexOf(arr, toreplace)] = "+"; 
                        }
                    }
                }
                test++;
                //Console.WriteLine(confStr.Length);
            }
                //res = arr.ToString();
                   // Do something with the current line
                //File.WriteAllLines("test.txt", );
            
            for (int oo = 0; oo < arr.Length; oo++)
            {
                Console.WriteLine(arr.Length);
                Console.WriteLine(arr[oo]);
            }
        
          
            Console.Read();

Recommended Answers

All 10 Replies

so no one can help me in my issue ? i have a small error in the code , but i can't find it .

what exception you are facing?

i'm not having an exception , but the code is not replacing the values inside the arrays as needed (that's mean inside the whole file because each line is an array), i described what i want in details in my first post.

ok. w8

check this code

try
            {

                string fName = @"D:\t.txt";//path to text file
                StreamReader reader = new StreamReader(fName);
                StringBuilder sb = new StringBuilder();
                String S;
                while ((S = reader.ReadLine()) != null)
                {
                  
                    String[] arr = S.Split('\t');
                     for (int ii = 0; ii < arr.Length; ii++)
                        {
                           
                            for (int jj = 0; jj < arr.Length; jj++)
                            {
                               
                               if ((arr[ii] == arr[jj]) && (ii != jj))
                               {
                                   arr[ii] = ";;;;;";
                                }
                                else if ((arr[jj].Substring(1)==arr[ii].Substring(1)) && (ii != jj))
                               {

                                   arr[ii] = "ttttt";
                               }
                               
                            }

                           if(ii==arr.Length-1)
                               sb.Append(arr[ii]);
                           else
                               sb.Append(arr[ii]+"\t");
                        }
                       sb.AppendLine();
                    
                       Console.ReadLine();
                      
                }
                   reader.Close();                   
                   StreamWriter outfile = new StreamWriter(@"D:\t.txt");
                   outfile.Write(sb.ToString());
                   outfile.Close();
            }


            catch (Exception e)
            {
                Console.WriteLine("Exception is : {0}", e.ToString());
                Console.ReadLine();
            }
          }

Hello ,

thanks for your reply.


but what i want is when the program find 2 or more duplicate in the same line(that have the same special character) is to replace all the duplicates with this special character(1 special character for all duplicate)


and in condition 2 , i want to check each line , if it has 2 duplicate character with a different preceding character(replace both of the duplicate with t)


but change made to the code is ignoring this.

this program replaces the duplicates with character ";" if they exactly match and replace with "t"/B] if the symbol is different i couldn't understand what are you saying can you give any example?

do you want to match lines? or Strings within a line?

i want to match strings with lines , that mean test each line alone for duplicates of both condition and made the changes , for example:

a1 ;AT1 ;AT2 ,CE1 ,CT3
a2 ;AT1 ;AT1 ;CE1 ;CT3
a3 ,CO1 ;CO1 ;CT2 ,CT3
a4 ,AW  ,AW  ,AW  ;BE

should become :

a1 ;AT1 ;AT2 ,CE1 ,CT3 // this stays the same
a2 ; ;CE1 ;CT3  //;AT1 and ;AT1 are replaced by ;(preceding special character)
a3 t ;CT2 ,CT3  //,CO1 and ;CO1 are replace by t(same string but different special character)
a4 , ;BE //  ,AW and ,AW and ,AW are replaced by ,(preceding special character)

this code does exactly he same thing you want

try
            {

                string fName = @"D:\t.txt";//path to text file
                StreamReader reader = new StreamReader(fName);
                StringBuilder sb = new StringBuilder();
                String S;
                
                while ((S = reader.ReadLine()) != null)
                {
                   
                    String[] arr = S.Split(' ');
                     for (int ii = 0; ii < arr.Length; ii++)
                        {
                           for (int jj = 0; jj < arr.Length; jj++)
                            {
                               if ((arr[ii] == arr[jj]) && (ii != jj))
                               {
                                   arr[ii] = char.ToString(arr[ii][0]);
                                  
                                }
                                else if ((arr[jj].Substring(1)==arr[ii].Substring(1)) && (ii != jj))
                               {
                                   arr[ii] = "t";
                               }
                               
                            }

                           if(ii==arr.Length-1)
                               sb.Append(arr[ii]);
                           else
                               sb.Append(arr[ii]+" ");
                        }
                       sb.AppendLine();
                      
                }
                   reader.Close();                   
                   StreamWriter outfile = new StreamWriter(@"D:\t.txt");
                   outfile.Write(sb.ToString());
                   outfile.Close();
            }


            catch (Exception e)
            {
                Console.WriteLine("Exception is : {0}", e.ToString());
                Console.ReadLine();
            }
          }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.