I'm attempting to write an application that searches for and displays optionally sized strings made up of optional characters (both determined by the user) from a file (of any type).
The goal is to have said strings displayed in a listBox control.
I'm fairly new to C#, so through this endeavor I've familiarized myself with the different methods of reading data from a file to be worked with programmatically.
The wall I've run myself into (regardless of method of reading respective file) is how to go about ignoring everything *not* specifically requested.
For example, the user (via my settings module, which I've yet to create - though its very likely that I'll be doing just that since arriving at this personal impasse) requests that only strings containing the characters a-Z are listed, and only if those characters are found in sequences ranging between 5 and 10 characters in length.
I'm confident that I know the logic to accomplish this, however, thus far, I can only invoke that logic using English, not C#. Ultimately, that is what I need help with. Converting the concept into C# code that I can implement between the reading of said file, and writing to a listBox.
I apologize in advance for the long-winded sermon. Any help on the matter (in any capacity) would be greatly appreciated. Even advice on the best method to use for a file reading process that yield the greatest efficiency in this case.

Edited 6 Years Ago by StrangeArray: n/a

There are many ways to manipulate strings in C#, but i think for what you are hoping to do Regular Expressions are probably best suited.
If you havent worked with them before they can be a bit daunting, but they are an incredibly powerful string parsing tool. Check out regular-expressions.info for a rough starting guide. Also check out Expresso which is a free regualr expression builder.

Here is a rough example that attempts to do what you have asked for. I expect it will not meet all of your needs, but hopefully it will serve as a good starting example of how you can translate your logic to manipulate the read in data and achieve the desired result in C#. As Ryshad pointed out, "regular expressions" is very robust and highly recommended because of that, but it can also create efficiency concerns and the learning curve is somewhat steep--it really depends on the complexity of whatever you are doing.

public static void Test()
        {
            List<string> values = BuildListBoxValues("abcd fdsag\0dkeowsloiekdlss saasfdasfd 098098", "qwertyuiopasdfghjklzxcvbnm", 5, 10);

            listBox1.Items.Clear(); // clear existing ListBox items...

            // add each acceptable string that was found to the ListBox...
            foreach (string value in values)
                listBox1.Items.Add(value);
        }

        public static List<string> BuildListBoxValues(string data, string charsAllowed, int minLength, int maxLength)
        {
            // List of acceptable strings we will return (to be used for ListBox)...
            List<string> values = new List<string>();
            int ndx = 0;

            // process entire length of data string...
            while (ndx < data.Length)
            {
                char c = data[ndx]; // get first character for loop below...
                string value = "";  // reinitialize our acceptable ListBox value holder...

                // build string value for addition to List<string>, checking each allowed character...
                while (charsAllowed.Contains(c))
                {
                    value += c; // append the allowed character...
                    ndx++;      // increment for next character check...
                    c = data[ndx]; // get next character...
                }

                // Does the string value meet our length criteria?
                if (value.Length >= minLength && value.Length <= maxLength)
                    values.Add(value); // add this value to the list...

                ndx++; // skip unwanted character and begin again checking for next value...
            }

            return values; // return the list of acceptable strings...
        }

Ryshad: Thank you for the referral. Regex seem unavoidable for my ultimate goals here, and it has been something I've been meaning to dive deeper into for a long time now. The site you've referred me to has already been invaluable.

DdoubleD: The sample of code you've posted is precisely what I needed to get my head back in the game. It has helped me immensely.

Thank you both for the quick responses and exceptional information. I am confident that because of your posts I will be able to complete this project.

Glad we were able to help. If you are going to be using regex i would definitely recommend expresso. Once you get used to it, the designer section makes it really easy to build your expressions and the testing facility is helpful too. Its made my life easier on more than one occasion :)

And feel free to post here if you get stuck with using regex, we'll be happy to help you get to grips with it.

Edited 6 Years Ago by Geekitygeek: n/a

This article has been dead for over six months. Start a new discussion instead.