繁体   English   中英

如何创建不带空格或空行的文本文件? 仅一小段文字

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

这就是我在文本文件的form1构造函数中使用的方式:

创建空的文本文件:

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

编码,因为它希伯来语内容并下载:

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

客户端= WebClient页面=字符串

然后我从页面中提取日期和时间以及文本:

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);

然后,我将带有空格的新内容写入文本文件test.txt:

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

combindedString =字符串

这是TextExtractor类,在这里我从页面中提取日期和时间以及文本:

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;
        }
    }
}

这是所有提取之后的文本文件test.txt:

文本文件

您可以看到第一行是空行,然后最细的文本行不是从左到左,从左开始是空白。 然后在每两行之间有一个空格/空行。

我想要的是文本文件将没有任何空间,不是从任何行开始也不在任何行之间,并且在顶部将没有第一个空行。

仅一小段文字。

这将为您解决:

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);               
                 }
          }
     }    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM