Search code examples
c#searchstreamfile-read

Searching for string in a Text file and returning some string in c#


How to parse the line of text :

File.txt:

n:96 pts:341998 pts_time:3.79998 pos:-1 fmt:yuvj420p sar:12/11

and just get the time value appearing after pts_time .

Expected Output :

3.79998

How to get the expected output?? Any help would be really appreatiated.


Solution

  • Split n:96 pts:341998 pts_time:3.79998 pos:-1 fmt:yuvj420p sar:12/11 with Space.

    string[] lineParts = line.Split(" ".ToCharArray());
    

    Get the array element which matches pts_time key.

    string ptsTime = lineParts.First(p => p.StartsWith("pts_time")); // pts_time:3.79998
    

    Split the ptsTime with :

    string ptsTimeValue = ptsTime.Split(':')[1]; // 3.79998