Search code examples
c#textxnastreamstreamreader

Reading a text file: 'If' character store information after character


What I'm trying to do is read in a text file that holds all the stats associated with a car part, such as weight, acceleration, top speed etc. I want to be able to simply write in the text file: W 5 A 10 T 20

Therefore i need to check if there is a character and if so grab the value after it. This is what I'm trying so far:

public virtual void ReadStats(string selectedModel) { List lines = new List();

        using (var ModelInfo = new FileStream(selectedModel, FileMode.OpenOrCreate))
        {
            using (StreamReader reader = new StreamReader(ModelInfo))
            {
                float input;

                string line = reader.ReadLine();
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("W"))
                    {
                       //Save the value after the character.
                    }
                }
            }
        }
    }

For some reason it isn't even getting into the 'if' it seems to not read the 'W' character. Also how would i go about grabbing the value after the character.

Thanks.


Solution

  • Any reason why you are not using substring?

    var test = line.substring("W")
    

    Put this in your if block.