How do I Split a string by empty lines? - c#

I want to split my string just by empty entries. I know you can remove these with the string options but in my situation I need them to split the string. Thank you!
string[] text = content.Split(' ');
For example you have this string:
Hi my Name is Max.
I'm 24 years old.
I like programming.
Split the string between old and I.
My Code:
WebClient client = new WebClient();
Stream stream = client.OpenRead("https://gist.githubusercontent.com/Quackmatic/f8deb2b64dd07ea0985d/raw/macbeth.txt");
StreamReader reader = new StreamReader(stream);
string[] text = reader.ReadToEnd().Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < 3; i++)
{
Console.WriteLine(text[i]);
}
Console.ReadLine();
The string is splitted by every new line. The problem is I want to have full paragraphs of my text.
My Output:
string[0] = ACT I.
string[1] = SCENE I. An open place.
string[2] =[An open place. Thunder and lightning. Enter three Witches.]
What I want.
string[0] = ACT I.
string[1] = SCENE I. An open place.
[An open place. Thunder and lightning. Enter three Witches.]
string[2] = FIRST WITCH.
When shall we three meet again
In thunder, lightning, or in rain?
or...
Doubtful it stood;
As two spent swimmers that do cling together
And choke their art. The merciless Macdonwald,
Worthy to be a rebel, for to that
The multiplying villainies of nature
Do swarm upon him, from the Western isles
Of kerns and gallowglasses is supplied;
And fortune, on his damned quarrel smiling,
Show'd like a rebel's whore. But all's too weak;
For brave Macbeth, well he deserves that name,
Disdaining fortune, with his brandish'd steel,
Which smok'd with bloody execution,
Like valour's minion,
Carv'd out his passage, till he fac'd the slave;
And ne'er shook hands, nor bade farewell to him,
Till he unseam'd him from the nave to the chaps,
And fix'd his head upon our battlements
I thought I could get the result by splitting the text by empty lines.
.

var foo = bar.Split(new string[] {Environment.NewLine + Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
It's important to have the combined Environment.NewLine + Environment.NewLine as this will only split when a new line is also followed by a new line (aka a blank line) and split accordingly
The result is:
foo[0] = "Hi my Name is Max.\r\nI'm 24 years old."
foo[1] = "I like programming."
Edit -- Based on your new input try give this a go:
var web = new WebClient();
var stream = web.OpenRead("https://gist.githubusercontent.com/Quackmatic/f8deb2b64dd07ea0985d/raw/macbeth.txt");
if (stream == null) return;
var reader = new StreamReader(stream).ReadToEnd();
var io = reader.Split(new string[] { "\n\n" }, StringSplitOptions.RemoveEmptyEntries);
From here you can iterate through and do what you please (ie. line[i].StartsWith("ACT") to put it into an object if you so wish)

if you are searching for empty lines you should do somthing like
"value".split(environment.newline+environment.newline)

Related

Using StreamReader split methode to create a txt file from a CSV file

I want to extract each string between the first "" for each row and create a text file with it.
sample CSV:
number,season,episode,airdate,title,tvmaze link
1,1,1,13 Sep 05,"Pilot","https://www.tvmaze.com/episodes/991/supernatural-1x01-pilot"
2,1,2,20 Sep 05,"Wendigo","https://www.tvmaze.com/episodes/992/supernatural-1x02-wendigo"
3,1,3,27 Sep 05,"Dead in the Water","https://www.tvmaze.com/episodes/993/supernatural-1x03-dead-in-the-water"
4,1,4,04 Oct 05,"Phantom Traveler","https://www.tvmaze.com/episodes/994/supernatural-1x04-phantom-traveler"
5,1,5,11 Oct 05,"Bloody Mary","https://www.tvmaze.com/episodes/995/supernatural-1x05-bloody-mary"
Final result .txt file:
Pilot
Wendigo
Dead in the Water
Phantom Traveler
Bloody Mary
my function:
private void GetEpisodeNamesFromCSV()
{
using (StreamReader sr = new StreamReader(AppDir + "\\list.csv"))
{
string strResult = sr.ReadToEnd();
string[] result = strResult.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
File.WriteAllLines(AppDir + "\\list_generated_" + ShowTitel + ".txt", result);
}
}
I can't figure out how to properly Split the stream reader object, to only get the names on each Line. I'm very new to programming, and this site helped me immensely! But this problem is specific, and I couldn't find the answer myself. I appreciate any help.
EDIT:
I went with the csvHelper solution suggested by #Jesús López:
// Create a List
List<string> episodeNames = new List<string>();
// Make sure ther are no empty lines in the csv file
var lines = File.ReadAllLines(AppDir + "\\list.csv").Where(arg => !string.IsNullOrWhiteSpace(arg));
File.WriteAllLines(AppDir + "\\list.csv", lines);
// Open the file stream
var streamReader = File.OpenText(AppDir + "\\list.csv");
var csv = new CsvReader(streamReader, CultureInfo.InvariantCulture);
// Read the File
csv.Read();
// Read the Header
csv.ReadHeader();
// Create a string array with Header
string[] header = csv.Context.Reader.HeaderRecord;
// Select the column and get the Index
var columnExtracted = "title";
int extractedIndex = Array.IndexOf(header, columnExtracted);
// Read the file and fill the List
while (csv.Read())
{
string[] row = csv.Context.Reader.Parser.Record;
string column = row[extractedIndex];
episodeNames.Add(column);
}
// Convert the List to a string array
string[] result = episodeNames.ToArray();
//write the array to a text file
File.WriteAllLines(AppDir + "\\list.txt", result);
This is not so much help on StreamReader as it is on strings
If you are confident of the file layout and format as shown (and that it will be consistent), try this quick-and-dirty in a Console app
:
var line = sr.ReadLine();
while (line != null)
{
if (line.Trim() == string.Empty) continue;
var lineEntries = line.Split(',', StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(lineEntries[4].Trim('"'));
line = sr.ReadLine();
}
Note that I offer this because of your statement "I am very new to programming" to show off methods string.Split() .Trim() (and check out .Join()) and how easy they make the basic logic of what you want to achieve.
Using a proper CSV reader is the best idea for a robust solution (plus data-integrity checking, exception handling etc), but there is a reciprocal danger of over-engineering, so if this code displays what you want/expect for a once-off learning experience, then go ahead and implement;-)

C# Syntax - Remove last occurrence of ';' in string from split

I have a list of strings stored in an ArrayList. I want to split them by every occurrence of ';'. The problem is, whenever I try to display them using MessageBox, there's an excess space or unnecessary value that gets displayed.
Sample input (variable = a):
Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;
Below is a line of code I used to split them:
string[] display_document = (a[0] + "").Split(';');
Code to display:
foreach (object doc_properties in display_document)
{
TextBox aa = new TextBox();
aa.Font = new Font(aa.Font.FontFamily, 9);
aa.Text = doc_properties.ToString();
aa.Location = new Point(pointX, pointY);
aa.Size = new System.Drawing.Size(80, 25);
aa.ReadOnly = true;
doc_panel.Controls.Add(aa);
doc_panel.Show();
pointY += 30;
}
The output that displays are the following:
How do I remove the last occurrence of that semicolon? I really need help fixing this. Thank you so much for all of your help.
Wouldnt It be easiest to check if the input ends with a ";" before splitting it, and if so remove the last character? Sample code:
string a = "Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
if (a.EndsWith(";"))
{
a = a.Remove(a.LastIndexOf(";"));
}
//Proceed with split
Split will not print last semicolon if no space character is added and your input is a string.
I don't know why you prefer an array list (which probably is the reason of this strange behaviour) but if you could use your input as a string you could try that
string a = "Arial;16pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
string[] display_document = a.Split(';');
foreach (object doc_properties in display_document)
{
//The rest of your code
}

Using StreamReader to read a .text file and split it up into arrays/classes

I need to use something like StreamReader to read a .text file and spit it out into arrays that could be used for a pictureviewer and option boxes, etc. The layout of the text file is something like:
PhotoURL PAGEURL SKU# Option1 Option2 Option3 .etc
[Edit]:
example of text file
http://image.com/book.jpg google.com PG52389 Hardcover Ebook
http://item.com/shirt.jpg google.com SH34920 Small Medium Large
ExamplePhotoUrlHere google.com SE39270 Grey Black Red Blue
Not every item has every single option, so there are some blanks on certain columns.
I know I need to use streamreader to read the text file, but I'm not sure how to split it into a class with arrays and all that.
Possible starting point (just splits on spaces):
var linesAsArrays = File.ReadAllLines(absoluteFilePath).Split();
You'll need to figure out what "column" mean - because from your sample it is unclear how to separate one from another.
Note that it may be better option to find existing CSV reader instead of inventing your own.
I am not going to write a complete solution, but you could do something like this (untested)
char[] separators = { ' ', '\t' };
while(!streamReader.EndOfStream)
{
string line = streamReader.ReadLine();
string[] fields = line.Split(separators, 4);
var result = new
{
PhotoUrl = fields[0],
PageUrl = fields[1],
Sku = fields[2],
Options = fields[3].Split(separators),
};
// Use result
}

Reading a text file and inserting information into a new object

So I have a text file with information in the following format, with the name, email, and phone number.
Bill Molan, Bill.Molan#gmail.com, 612-789-7538
Greg Hanson, Greg.Hanson#gmail.com, 651-368-4558
Zoe Hall, Zoe.Hall#gmail.com, 952-778-4322
Henry Sinn, Henry.Sinn#gmail.com, 651-788-9634
Brittany Hudson, Brittany.Hudson#gmail.com, 612-756-4486
When my program starts, I want to read this file and make each row into a new Person(), which I will eventually add to a list. I am wanting to read each line, and then use the comma to separate each string to put into the constructor of Person(), which is a basic class:
public PersonEntry(string n, string e, string p)
{
Name = n;
Email = e;
Phone = p;
}
I have done some looking and I think that using a streamreader is going to work for reading the text file, but I'm not really sure where to go from here.
You can use the following method:
string line;
List listOfPersons=new List();
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(#"c:\yourFile.txt");
while((line = file.ReadLine()) != null)
{
string[] words = line.Split(',');
listOfPersons.Add(new Person(words[0],words[1],words[2]));
}
file.Close();
Assuming that the comma will never appear within the data:
Use StreamReader.ReadLine to read each line of text. With each line of text, use string.Split to split the line into an array of strings using the comma as the split character. Now you have an array of 3 strings where [0] is the name, [1] the email, and [2] the phone.
You can read all lines as below // assuming all lines will have 3 values always
var allLines = File.ReadAllLines(path);
var listOfPersons = new List<Person>();
foreach(var line in allLines)
{
var splittedLines = line.Split(new[] {","})
if(splittedLines!=null && splittedLines.Any())
{
listOfPersons.Add( new Person {
Name = splittedLines[0],
Email = splittedLines .Length > 1 ?splittedLines[1]:null,
Phone = splittedLines .Length > 2? splittedLines[2]:null,
});
}
}
this code is a sample must be checked for various conditions like array length etc also please check the

parse lines using linq to txt

var t1 = from line in File.ReadAllLines(#"alkahf.txt")
let item = line.Split(new string[] {". "}, StringSplitOptions.RemoveEmptyEntries)
let verse = line.Split(new string[] { "\n. " }, StringSplitOptions.RemoveEmptyEntries)
select new
{
Index = item,
Text = verse
};
having problems with above code im unsure how to parse the lines properly.
the format of the file is like so, I would also like to ignore any empty lines
StringSplitOptions.RemoveEmptyEntries doesn't work for some reason
1. This is text it might have numbers
2. I skipped a line
In the LINQ part, you are inside a single line, so you might want to exclude the empty lines first:
from line in File.ReadAllLines(#"alkahf.txt")
where !string.IsNullOrEmpty(line)
You then do two splits - one on newline, which is odd (since that won't be there, since we know we are reading lines). I expect you mean something like:
let parts = line.Split('.')
where parts.Length == 2
select new {
Index = parts[0],
Text = parts[1]
};
?
Also, note that ReadAllLines is a buffered operation; if you want true streaming, you might want something like:
public static IEnumerable<string> ReadLines(string path) {
using(var reader = File.OpenText(path)) {
string line;
while((line = reader.ReadLine()) != null) {
yield return line;
}
}
}
which is not buffering (you don't load the entire file at once). Just change the first line to:
from line in ReadLines(#"alkahf.txt")
Thanks to Marc's answer I fixed my issue. Sorry for the late response I'm working on this as a personal project.
The code is like so
var t1 = from line in StreamReaderExtension.ReadLinesFromFile(#"alkahf.txt")
let parts = line.Split(new string[]{". "},
StringSplitOptions.RemoveEmptyEntries)
where !string.IsNullOrEmpty(line)
&& int.Parse(parts[0].ToString()).ToString() != ""
select new
{
Index = parts[0],
Text = parts[1]
};
The int parse addition makes sure that the input is returning an integer, if you're using this code it's a good idea to set a flag in case it picks ups a non-integer or it will go unnoticed.

Categories

Resources