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

Posted on 2007-10-12
Medium Priority
Last Modified: 2010-04-15
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();
Question by:Karl66
  • 2
LVL 19

Accepted Solution

Limbeck earned 1000 total points
ID: 20066887
use length()

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

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 total points
ID: 20066907
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).

LVL 19

Expert Comment

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

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

you are right of course :)


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question