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

Posted on 2007-10-12
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
    LVL 19

    Accepted Solution

    use length()

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

    Assisted Solution

    by:Bob Learned
    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

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

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

    you are right of course :)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now