looking for a brokerage account or IRA... click here Add To Favorites
return to index 

C# Needle In Haystack String Replacements

The following program will take a text file and replace all the needles with a replacement string. The needles are supplied through another text file where each line is a needle. The result will then be outputted to a new file. The class for the program is below. To execute the class in your static main, simply use the following commands.

        static void Main(string[] args)
        {
            StringUtility su = new StringUtility();
            su.Execute();
        }

Click Here To Download the Compiled Code

The code for the class is as follows. Pay special attention to the StreamWriter, StreamReader, and Regex used to make the replacements. The use of a MatchEvaluator may be new to you as well. It simply returns the replacement string for the needles.

    class StringUtility
    {
        private Hashtable users = new Hashtable();
        private string replacement = "";
        public StringUtility()
        {

        }
        public void Execute()
        {
            string filename="", output="";
            Console.WriteLine("File with needles: ");
            filename = Console.ReadLine();
            this.LoadNeedles(filename);
            Console.WriteLine("File with haystack: ");
            filename = Console.ReadLine();
            Console.WriteLine("Replacement string: ");
            replacement = Console.ReadLine();
            Console.WriteLine("File to output to: ");
            output = Console.ReadLine();
            this.ProcessFile(filename, output);
        }
        private void LoadNeedles(string filename)
        {
            Console.WriteLine("Started loading needles");
            try
            {
                FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(file);
                string line;

                while (sr.Peek() >= 0)
                {
                    line = sr.ReadLine();
                    if(!users.ContainsKey(line.Trim()))
                        users.Add(line.Trim(),1);
                }
                file.Close();
                sr.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.WriteLine("Done loading needles");
        }
        private void ProcessFile(string filename, string output)
        {

            try
            {
                Console.WriteLine("Started loading body");
                FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(file);
                FileStream outputfile = new FileStream(output, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(outputfile);
                string line;
                string body = "";
                Regex r;
                MatchEvaluator myEvaluator = new MatchEvaluator(this.ReplaceCC);

                while (sr.Peek() >= 0)
                {
                    line = sr.ReadLine();
                    body = body + line + "
";
                }
                file.Close();
                sr.Close();
                Console.WriteLine("Done loading body");
                Console.WriteLine("Started replacements");
                int i = 0;
                foreach (DictionaryEntry needle in users)
                {
                    r = new Regex(needle.Key.ToString(), RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    body = r.Replace(body, myEvaluator);
                    i++;
                    if(i%1000==0)
                        Console.WriteLine("Iterand: {0}",i.ToString());
                }
                Console.WriteLine("Done replacements");
                sw.Write(body);
                Console.WriteLine("Done writing body to file");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        private string ReplaceCC(Match m)
        {
            return replacement;
        }


    }

Additional Interesting Articles

Block File Leechers Using PHP
C# Regular Expression Example
SQL Benchmarking - Clear Cache
C# DataSet v DataReader
Stepwise Linear Regression in t-SQL
VB.net Regex Example In SSIS

©2008 AndrewKimball.com