Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been searching for about an hour and simply can't find any solutions to this. I want each row from my .txt-file to be added to my list as long as it starts with the same number. The problem lies within "line.Substring(0, 1)" and i dont know how to solve it. Perhaps you understand what i want to acheive.

        using (StreamReader file = new StreamReader(schedulePathDays))
        {
            string line;
            while ((line.Substring(0, 1) = file.ReadLine()) == thisDay.ToString())
            {
                exercisesRow.Add(line);
            }
        }
share|improve this question
Why not string.StartsWith()? – Lloyd 22 mins ago
line seems to be null... – Sebastian Piu 22 mins ago
(line.Substring(0, 1) = file.ReadLine()) is wrong. Did you mean (file.ReadLine().Substring(0, 1))? – Nolonar 22 mins ago
@TimSchmelter He's calling line.SubString before even assigning anything to line. – Nolonar 16 mins ago

3 Answers

You can use ReadLines and then use LINQ to filter:

var result = File.ReadLines("yourPath")
                 .Where(line => line.StartsWith("1"));

ReadLines is deferred execution so it can work in large text file.

Edit: To map with your code, replace your code with below:

exercisesRow = File.ReadLines(schedulePathDays)
                   .Where(line => line.StartsWith(thisDay.ToString()))
                   .ToList();
share|improve this answer
2  
+1 for mentioning deferred execution – Nolonar 19 mins ago
Im very new to C# and therefor i don't know how to use the code you just posted. Could you post how my code would look with your code included? thanks! – Arvin Ashrafi 14 mins ago
@ArvinAshrafi: Edited my answer – Cuong Le 11 mins ago
@ArvinAshrafi Keep in mind, that you shouldn't blindly copy-paste other people's code. Try to understand the code first. For instance: File should actually be file (lower-case) and StartWith should be StartsWith (with 's' in between). – Nolonar 9 mins ago
@Nolonar: thanks for pointing out StartsWith :), but File should not change to file since ReadLines is static method – Cuong Le 8 mins ago

Your syntax doesn't make sense. You might want something like:

file.ReadLine().Substring(0,1)==thisDay.ToSTring()

I think this is what you wanted to write though Cuong Le's answer is probably a lot nicer (except needing parameterisation).

share|improve this answer

Your Substring(0,1) is in the wrong place. It should come after line is assigned.

//Altnerately: (...).Substring(0,1) instead of (...)[0]
while((line = file.ReadLine())[0] == thisDay.ToString()[0]){
    //code
}

It makes no sense to ask try to assign the result of the Substring method.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.