Reading All contents of a text document line by line - c#

I have a text document with multiple lines which contain a persons name, age, favourite number and their favourite activity.
For example:
Line 1: Josh 30 100 Likes to play soccer.
Line 2: May 21 3 Likes to dance.
etc.
I have a message box which I would like to show their name, age, favourite number and activity one by one.
How can I do this and how can I pick up their number as integers too? thanks.
Sorry I have no code as I don't know what to do but I was thinking of using streamreader.

Here are the tools you need for your task:
For reading the lines of the file (Blorgbeard's suggestion):
string[] lines = File.ReadAllLines("file.txt");
For getting the values of each line (assuming that the names can be one-word only):
string[] values = line.Split(new char[]{' '},4);
For converting a value from String to Integer:
int intValue = Convert.ToInt32(strValue);

Related

Read Text with conditions

sample1.txt
ID Name Age
1 Amila 31
2 Jaya 28
3 Sahan 24
4 Ishara 21
This is my sample1.text file with 3 columns. I want to read the file and write only persons with age greater than 25.
Can you please help to with the best way to do this.
Thanks
Use the string.Split method to add your elements to an array or list.
For for tabs it'd look like yourString.Split("\t") and for commas yourString.Split(",")
More info on the Split here: https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.8
How I would tackle this
Read the lines from the sample file using File.ReadAllLines(#"FilePath") and store them in a list.
Create a StreamWriter for writing the values you want to the Output File
If you want the headers in the output file then write the headers and then remove them from the list.
Have a loop that will split the line with line.split('\t'), check the age for that line, and write the line if it matches the condition using output.writeline("Stuff to write")
After Looking at Your Progress
Move the using(var writer=new StreamWriter(WritePath)) to above string s =""; since you are only writing the last line of the original file.
You are basically overwriting the file with each line read instead of appending.
Inside the while loop you can check the age and then write the lines you want in the format you want.

C# - Tab Delimited String not working if column has empty characters/spaces

I have the following text in a .txt file:
hello 123 example info
mello 456 xample text
yello maple syrup
I am doing:
string FILENAME = Server.MapPath(".") + #"\example.txt";
string[] allLines = File.ReadAllLines(FILENAME);
string[] items = allLines[i].Split('\t');
Now, for the last row, I am getting two consecutive empty columns instead of just skipping the second one (items[1]) and giving me the third one.
My final result looks like:
hello 123 example info
mello 456 xample text
yello
I am actually inserting the information in a database but I don't want to go into much detail because I know that the problem is in the split itself.
My final result should include the third column information in the third row.
You are splitting the line by '\t' which is correct as long as the blank space uses tabs to get there. If they had used tabs properly, you should have ended up with exactly what you wanted. If they had used spaces, instead, you would have ended up with all the text in your first column, just very long. What you got instead, is nothing. The most likely solution is that there were actually EXTRA tabs, that the output array was longer than expected, and the third column is now, for instance, in the items[3] slot instead of items[2]. It is difficult to tell you how you can control for this except that instead of checking items[2] you could test for items[items.length-1].

How to tell which delimiter string was split on

I'm trying to parse out line items from text extracted from a PDF. The text extracted comes out poorly formatted and in one long string per page. There aren't any useful delimiters, but the lines start with one of two strings. I've set up the Split() using a string array with both of those strings, but I need to know which delimiter the elements were split on.
I found this link, but I'm not that great at RegEx. Can someone assist in writing the RegEx string?
var lineItems = page.PageText.Split(new string[] { "First String Delimiter", "Second String Delimiter" }, StringSplitOptions.None);
What I need is to know is if element[x] was a result of "First String Delimiter" or "Second String Delimiter".
EDIT: I don't care if Regex is the solution. Linq may be equally suited. Linq didn't come out until after I earned my degrees, so I'm similarly unfamiliar with it.
Imagine a page with about 15-20 of these end to end coming back as one long string with no carriage returns: Since they all start with "Corporate Trade Payment Credit" or "Preauthorized ACH Credit", I can split on those, but I need to know what type it was.
Preauthorized ACH Credit (165) 10,000.00 489546541 0000000000 Text Some long description about transaction- Preauthorized ACH Credit (165) 5,310.99 8465498461 0000000000 Text Another long description Corporate Trade Payment Credit (165) 4,933.17 8478632458775 0000000000 Text Another confidential string description.
Why don't you just run the split twice, once with the first delimiter, then again with the second delimiter?
var firstDelimiterItems = page.PageText.Split("First String Delimiter");
var secondDelimiterItems = page.PageText.Split("Second String Delimiter");
Sometimes the simplest solutions are the best ones. Don't know why this didn't occur to me earlier.
var pageText = page.PageText.Replace("Corporate Trade Payment", "\r\nCorporate Trade Payment").Replace("Preauthorized ACH Credit", "\r\nPreauthorized ACH Credit");
This gives me the line items on their own lines. No Regex needed. Thank you all for your help, and if you find a way to the original question with Regex, please post. I'm always up to learning more.

C# read from text file and store in variables

I have a text file that reads
1 "601 Cross Street College Station TX 71234"
2 "(another address)"
3 ...
.
.
I wanted to know how to parse this text file into an integer and a string using C#. The integer would hold the S.No and the string the address without the quotes.
I need to do this because later on I have a function that takes these two values from the text file as input and spits out some data. This function has to be executed on each entry in the text file.
If i is an integer and add is the string, the output should be
a=1; add=601 Cross Street College Station TX 71234 //for the first line and so on
As one can observe the address needs to be one string.
This is not a homework question. And what I have been able to accomplish so far is to read out all the lines using
string[] lines = System.IO.File.ReadAllLines(#"C:\Users\KS\Documents\input.txt");
Any help is appreciated.
I would need to see more of your input data to determine the most reliable method.
But one approach would be to split each address into words. You can then loop through the words and find each word that contains only digits. This will be your street number. You could look after the street number and look for S, So, or South but as your example illustrates, there might be no such indicator.
Also, you haven't provided what you want to happen if more than one number is found.
As far as removing the quotes, just remove the first and last characters. I'd recommend checking that they are in fact quotes before removing them.
From your description, every entry has this format:
[space][number][space][quote][address][quote]
Here is some quick and dirty code that will parse this format into an int/string tuple:
using namespace System;
using namespace System.Linq;
static Tuple<int, string> ParseLine(string line)
{
var tokens = line.Split(); // Split by spaces
var number = int.Parse(tokens[1]); // The number is the 2nd token
var address = string.Join(" ", tokens.Skip(2)); // The address is every subsequent token
address = address.Substring(1, address.Length - 2); // ... minus the first and last characters
return Tuple.Create(number, address);
}

LINQ; Select Groups of Lines from a Text File

Greeting Gurus, I have a text file with groups of text that I need to seperate into seperate string arrays. How do I group these using the line "start:" as the delimiter? (groups may contain differing line counts but the all begin with "start:". Is this a job that LINQ can accomplish? (Each line has seperate data, it does not look that way in the preview).
List<string> lines = File.ReadAllLines(#"C:\test.txt").ToList();
start: data 1
boy: data 2
great: data 3
start: data 7
boy: data 8
great: data 9
girl: data 10
may be there is a linq solution but when you simply can handle this with for loops, I think there isn't any need to linq, for example I think bellow code done what you want:
var groups = new List<List<string>>();
foreach (var line in lines)
{
if (line.StartsWith("Start:"))
{
groups.Add(new List<string>());
}
groups[groups.Count - 1].Add(line);
}
I'd assumed first line of your file starts with Start: else you should skip first lines until reach the first start:.
File.ReadAllText(#"C:\test.txt").split("start:");
Will read the file and split the file into an aray of string on the start: tag.

Categories