Shawn
Shawn

Reputation: 11

Looking for a tag in a txt doc

Ok I tried all the things that you all sugested, but it is still not working for me. I am not sure why but it will not kick out the output sometimes and sometimes it it puts an out put that is not right. What am I doing wrong.

 InPutBox = Input.Text;
        int x = 1;
        var lines = Input.Text.Split(new string[] { Environment.NewLine },StringSplitOptions.None);

        for (var index = 0; index < lines.Length; index++)
        {

            var line = lines[index];

            do
            {
                x++;
                System.Console.WriteLine("'{0}'", InPutBox);
                bool test1 = InPutBox.StartsWith("11600");
                bool test2 = InPutBox.EndsWith("(");
                if (test1 && test2)
                {
                    int first = InPutBox.IndexOf("11600");
                    int last = InPutBox.LastIndexOf("(");
                    InPutBox = InPutBox.Substring(first, last - first);
                }
            }
            while (x < 50);
            System.Console.WriteLine("'{0}'", line);
            if ((line.StartsWith("11600") && line.EndsWith("(")))
            {
                MessageBox.Show("These are errors in line" + index + ": " + line);
                break;
            }

        }

Upvotes: 0

Views: 61

Answers (3)

Marcus Ha&#223;mann
Marcus Ha&#223;mann

Reputation: 118

private void InPutBoxMethod()
    {
        // split text into lines
        var lines = textBox1.Text.Split(new string[] {Environment.NewLine}, StringSplitOptions.None);

        // loop through all lines and check for errors
        for (var index = 0; index < lines.Length; index++)
        {
            var line = lines[index];
            System.Console.WriteLine("'{0}'", line);
            if ((line.StartsWith("W") && line.EndsWith("0")))
            {
                MessageBox.Show("These are errors in line " + index + ": " + line);
                break;
            }
        }
    }

Upvotes: 0

nickles80
nickles80

Reputation: 1101

Check your conditions. With your current code, .Substring will throw an error if last is not found (because startIndex cannot be less than zero).

You probably want this:

bool test1 = InPutBox.StartsWith("W");
bool test2 = InPutBox.EndsWith("0");
if (test1 && test2) {
    int first = InPutBox.IndexOf("W");
    int last = InPutBox.LastIndexOf("0");
    InPutBox = InPutBox.Substring(last, first - last);
}

@D Stanley is probably right in that the last line is meant to be reversed as well

InPutBox = InPutBox.Substring(first, last - first);

Upvotes: 1

D Stanley
D Stanley

Reputation: 152644

This:

InPutBox = InPutBox.Substring(last, first - last);

probably needs to be this:

InPutBox = InPutBox.Substring(first, last - first);

May need to add or subtract 1 depending on whether or not you want to include the W or 0. Run it in the debugger to see the actual numbers and you should be able to diagnose more quickly.

Upvotes: 4

Related Questions