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?


            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();
Who is Participating?
LimbeckConnect With a Mentor Commented:
use length()

num = input.Substring(indexOne + 6, input.Length());
Bob LearnedConnect With a Mentor Commented:
There is an overloaded method for Substring that just takes a single argument--start index--which gets the rest of the string starting from that index (without the need for length).

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

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

you are right of course :)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.