Link to home
Start Free TrialLog in
Avatar of Karl66
Karl66

asked on

C# using "num = input.Substring(indexOne + 6, 53);" to get the rest of the string?

I am the following code to find a keyword in a text file. When I find it I want to capture the rest of the line. But some lines are 53 and some are 56 and maybe longer. If I put in 56 it errors out on lines that are only 53. Any thoughts?

Karl66

            if (File.Exists(path + @"\tmp\ssm.txt"))
            {

                int indexOne = -1;  // ondex of ser #
                int indexTwo = -1;  // index of a space after a number
                string num = "";

                // Read file to list
                StreamReader sr = File.OpenText(path + @"\tmp\ssm.txt");
                string input = null;
                while ((input = sr.ReadLine()) != null)
                {
                    indexOne = input.IndexOf("Host  UDO");
                    if (indexOne > -1)
                    {
                        num = input.Substring(indexOne + 6, 53);
                        break; // exit loop
                    }

                }
               
                lblLibrary.Text = num; // not really a num
                return path;
                // sr.Close();
            }
ASKER CERTIFIED SOLUTION
Avatar of Limbeck
Limbeck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Limbeck
Limbeck

Ah a very subtle way to point out my error Bob :)

num = input.Substring(indexOne + 6, input.Length);

you are right of course :)

Ed.