Reputation: 13
I have a text file and within it, it looks like:
xxxx:value
All I want to read is the value, I've tried to manipulate:
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
But I haven't had much luck in doing that, any help would be brilliant.
Upvotes: 0
Views: 1782
Reputation: 4636
You'll need to loop through each line and split each line... Probably should check that each line isn't null and that it does contain a colon... but that could be unnecessary depending on your data...
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
while (!sr.EndOfStream)
{
String line = sr.ReadLine();
if (line != null && line.Contains(":"))
Console.WriteLine(line.Split(':')[1]);
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
Upvotes: 2
Reputation: 4212
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line = sr.ReadToEnd();
string[] array = line.Split(':');
Console.WriteLine(array[1]);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
Upvotes: 0