'How can I read a txt file line by line and print a specific object from that line? c#
Example txt file.
Main 98767 92,2838 923989382 dog cat -123 9838
jason main data form print
Someone can help me how can read specific object from each line. For example the output: Main 922838 dog cat data print
Solution 1:[1]
I do not know what kind of objects would you like to create and what is the object separator. If the list of strings is good enough and the separator is space, here is a code snippet...
static void Main()
{
var lines = System.IO.File.ReadAllLines("data.txt");
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line)) continue;
var objects = line.Split(' ').ToList();
objects.ForEach(x => Console.WriteLine(x));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tomas Paul |
