user3596190
user3596190

Reputation: 257

How do i create the text file to be without spaces or empty lines ? Just one block of text

This is how i use in form1 constructor with the text file:

Create empty text file:

ww = new StreamWriter(@"c:\temp\test.txt");

Encoding since it's hebrew the content and downloading it:

client.Encoding = System.Text.Encoding.GetEncoding(1255);
page = client.DownloadString("http://rotter.net/scoopscache.html");

client = WebClient page = string

Then i extract the date and time and the text from the page:

TextExtractor.ExtractDateTime(page, newText, dateTime);
StreamWriter w = new StreamWriter(@"d:\rotterhtml\rotterscoops.html");
w.Write(page);
w.Close();
TextExtractor.ExtractText(@"d:\rotterhtml\rotterscoops.html", newText, dateTime);

Then i write the new content with the spaces to a text file test.txt:

combindedString = string.Join(Environment.NewLine, newText);
ww.Write(combindedString);
ww.Close();

combindedString = string

And this is the class TextExtractor where i extract the date and time and text from page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace ScrollLabelTest
{
    class TextExtractor
    {
        public static void ExtractText(string filePath, List<string> newText, List<string> dateTime)
        {
            //newText = new List<string>();
            List<string> text = new List<string>();
            var htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.OptionFixNestedTags = true;
            htmlDoc.Load(filePath, System.Text.Encoding.GetEncoding(65001));

            if (htmlDoc.DocumentNode != null)
            {
                var nodes = htmlDoc.DocumentNode.SelectNodes("//a/b");
                foreach (var node in nodes)
                {
                    text.Add(node.InnerText);
                }
            }
            List<string> t = filterNumbers(text);
            for (int i = 0; i < t.Count; i++)
            {
                newText.Add(t[i]);
                newText.Add(dateTime[i]);
                newText.Add("");
            }
        }

        public static void ExtractDateTime(string text, List<string> newText, List<string> dateTime)
        {

            //dateTime = new List<string>();
            string pattern1 = "<span style=color:#000099;>(?'hebrew'[^<]*)</span>";
            Regex expr1 = new Regex(pattern1, RegexOptions.Singleline);
            MatchCollection matches = expr1.Matches(text);
            foreach (Match match in matches)
            {
                string hebrew = match.Groups["hebrew"].Value;

                string pattern2 = @"[^\s$]*:[^:]*:\s+\d\d:\d\d";
                Regex expr2 = new Regex(pattern2);
                Match match2 = expr2.Match(hebrew);
                string results = match2.Value;
                int i = results.IndexOf("שעה");
                results = results.Insert(i + "שעה".Length, " ");
                dateTime.Add("דווח במקור " + results);
            }
        }

        private static List<string> filterNumbers(List<string> mix)
        {
            List<string> onlyStrings = new List<string>();
            foreach (var itemToCheck in mix)
            {
                int number = 0;
                if (!int.TryParse(itemToCheck, out number))
                {
                    onlyStrings.Add(itemToCheck);
                }
            }
            return onlyStrings;
        }
    }
}

And this is the text file test.txt in the end after all the extraction:

Text File

You can see the first line is empty line then the fiest text line not beging from the left first left side but there is a space from the left side. Then between each two lines there is a space/empty line.

What i want is that the text file will be without any space not from any line beginning and not between any line/s and that in the top there will be no first empty line.

Just one block of text.

Upvotes: 0

Views: 2112

Answers (1)

Omri Aharon
Omri Aharon

Reputation: 17064

This will fix it for you:

using (StreamWriter sw = new StreamWriter(@"C:\temp\test1.txt", false))
{
     using (StreamReader sr = new StreamReader(@"C:\temp\test.txt"))
     {
          while (sr.Peek() >= 0)
          {
                 var strReadLine = sr.ReadLine().Trim().Replace("\t", "").Replace("\r\n", "");
                 if (!String.IsNullOrWhiteSpace(strReadLine)) 
                 {
                        sw.WriteLine(strReadLine);               
                 }
          }
     }    
}

Upvotes: 1

Related Questions